Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CollectionBase Class / Interface of objects

Status
Not open for further replies.

jantie78

Programmer
Jan 4, 2002
58
0
0
KR
Hi all,

I discovered yesterday something cool you can do with a class inherited from the CollectionBase class. First I made a simple class used to store data (for example a customer) :

Public Class Customer
Private sName as String
Private sTelephone as String

Public Property Name As String
'Some code...
End Property
Public Property Telephone As String
'Some more code...
End Property

Public Sub New(aName As String, aTelephone As String)
Name = aName
Telephone = aTelephone
End Sub
End Class Customer

Then I've created a class that acts as a Collection for my Customer class (Strong typed collection), like this :

Public Class CustomerCollection
Inhertis CollectionBase

Public Default Property Intem(Index As Integer)
Get
Return CType(List.Item(Index), Customer)
End Get
Set (ByVal Value As Customer)
List.Item(Index) = Value
End Set
End Property

Public Function Add(ByVal Item As Customer) As Integer
Return List.Add(Item)
End Function
End Class

Now comes the cool part, I added a few Customers to my Collection class and then I set the DataSource property of a DataGrid (found this trick on the net). And my DataGrid displays nicely all my customers of my collection with all their properties (Name and Telephone.

And now comes the question : how is this done?? I understand the working of the collection, but I can't figure out how my datagrid knows what properties my Customer object has. In VB6 I think this could be done by querying the IUnknown Interface. Is there a sortlike mechanism in (VB).NET ??

Thanks in advance,
Jan
 
On the MSDN CD, see Net Development / Net Framework / Reference / Class Library / System.Reflection / MemberInfo Class (MethodInfo Class, PropertyInfo Class, EventInfo Class). Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
The collectionbase
Implements IList, ICollection, IEnumerable

these are the interfaces that must be implemented. It takes only the IList interface so that datagrid.datasource works, if its implemented it will show you data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top