• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Custom collections - how do I enable the default property to be invoked using an index or a string?

Hi Experts,

You know collections that can be referenced by using either of the following syntax;

foo = myObject("itemID").value

OR

foo = myObject(0).value

How do you do this?

At the moment I'm using the line;

Inherits ArrayList

to allow me to use the .Add(object) function and give me an enumeratable (is that a word?) object.
0
thefritterfatboy
Asked:
thefritterfatboy
1 Solution
 
enwhyseeCommented:
What you'd like to create is called an "indexer". You could use syntax like this to define one:

      Default Public Property Item(ByVal Index As Int32) As MyClass
            Get
                  Return something
            End Get
      End Property

      Default Public Property Item(ByVal Key As String) As MyClass
            Get
                  Return something
            End Get
      End Property

This article gives a more detailed code sample:
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B311323

The "Item" property is what does the trick here. Documentation for http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vamthItem.asp

0
 
thefritterfatboyAuthor Commented:
Spot on, thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now