Question List(of T) with items accessible by name and index

jeanjeanot

New member
Joined
Oct 17, 2011
Messages
2
Programming Experience
3-5
Hi all,
I want to build my own class. In it, I need to recreate a behavior we can see in DataTable and in some other places : accessing items by their name or by their index. How can I reproduce such a behavior. Here's what I mean :

'Here's one way to access the data
A = MyObj(0).value
'Here's the other way I would like to acces it
A = MyObj("Blablabla").value

How can I create my class so I can access items in an array (or list preferably) by it's index or an unique Name ?
Thank you !

Jean
 
Inherit Collection(Of T) when making a new collection if you want to override existing members, or to allow others to do that if they inherit your class.
You need to add a property that takes a String parameter.
To access a property without actually writing it in code you have to declare the property Default. Since there is already a default property named Item, you use that name too and declare your property Overloads.
 
Note that inheriting Collection(Of T) will give you a class that behaves much like List(Of T), which will not give you the dictionary-like behaviour, i.e. retrieval by key, that you want. The KeyedCollection(Of TKey, TItem) class has both retrieval by index and retrieval by key.
 
Thanks guy for the fast replies. It is exactly what I was looking for. I didn't know that a property could have arguments!!! I've learned to code by myself and I'm always suprised to see how far I went in some area of coding and still not knowing the basic things in others...
 
Back
Top