Link to home
Start Free TrialLog in
Avatar of eventprostrategies
eventprostrategies

asked on

Implement Default Class Method?

int i = myCollection[0];  //  Dim i As Integer = myCollection(0);

i don't know what it's called ... prob not "default class method" ... but i want to know howto do this!  When you have objects with lots of collections within them ..., myCollection.Item[0].anotherCollection.Item[0] ... getting rid of ".Item" is an awesome shortcut.

ListViewItemCollections and some other things implement this ... how can i?  i haven't seen it documented anywhere and i have yet to correctly guess how it's done.

tia.

Avatar of eventprostrategies
eventprostrategies

ASKER

doh!  i Always figure these things out right After i post ...

this is cool tho, for anyone else who's wanted their collection class to have an indexer to allow myCollection[0] as a shortcut to the Item property.

http://www.codeguru.com/vb/gen/vb_general/indexers/article.php/c6079/
also, that article only has it in vb.

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

----

c# would be

public InventoryItem this[int index]
 {
  get
    { return (InventoryItem)base.List[index]; }
  set
    { base.List[index] = value; }
 }
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial