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
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