• 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


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.
1 Solution
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
                  Return something
            End Get
      End Property

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

This article gives a more detailed code sample:

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

thefritterfatboyAuthor Commented:
Spot on, thank you!

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