Question Individual Elements in a Collection

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
Array access in a collection

I'm converting a library from C# to VB.net which so far hasn't been too difficult. One class though implements IEnumerable and IEnumerator and contains a block of code like this:
VB.NET:
public class MyClassCollection
{
        // Assume all other necessary code is in place
        protected ArrayList _objMyClassList = new ArrayList();
        public MyClass this[int index]
        {
                get { return (MyClass)_objMyClassList[index]; }
                set { _objMyClassList[index] = (object)value; }
        }
}
This is obviously what allows me to access individual elements within the collection by simply appending the [] brackets to the variable name instead of having to expose _objMyClassList through a property or using a function.

I'm not sure how to do the same thing in VB. So...um...how do you do that in VB.net?
 
First of all, this is essentially a repost of a previous question. I'm hoping that this attempt is a little more clear, since my original post got no responses. Thanks for indulging me here.

I've written in C# for a long time, but now I need to write some things in VB.net which I don't have as much experience with. Mostly what I'm writing is stuff I've done before in C# and it's usually just a matter of adjusting the syntax which isn't hard. However, there is one thing that I can do in C# that I can't figure out in VB.

One common object that I create is (Object)Collection object, which is just an enumerated collection of Objects so that I can easily loop through them. In C# I use the following to allow access to individual elements in that collection:
VB.NET:
protected ArrayList _MyClassList = new ArrayList(); // This is the list that I use to store the elements

public MyClass this[int index] {
    get { return (MyClass)_MyClassList[index]; }
    set { _MyClassList[index] = (object)value; }
}
This allows me to access individual elements with something like
VB.NET:
MyClassCollection MyClasses = new MyClassCollection();
MyClasses.Add(new MyClass());

MyClasses[0].SomeProperty = 1;
Is there something equivalent in VB to this? Or will I have to just continue doing what I'm doing now, which is something like this:
VB.NET:
Public Property ItemByIndex(ByVal index As Int32) As MyClass
    Get
        Return CType(_MyClassList(index), MyClass)
    End Get
    Set(ByVal value As MyClass)
        _MyClassList(index) = CType(value, Object)
    End Set
End Property

' Which allows me to access individual elements like this:
MyClasses.ItemByIndex(0).SomeProperty = 1

' But I'd much rather be able to access it like this:
MyClasses(0).SomeProperty = 1
Is there any way to make it so that I can access individual elements using the second method above rather than the first method? IMHO it would make the code "prettier" and more readable (unfortunately for an OCD like me an important concern)
 
Back
Top