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].anoth erCollecti on.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.
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].anoth
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.
ASKER
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[i ndex]; }
set
{ base.List[index] = value; }
}
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[i
set
{ base.List[index] = value; }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/