Hi All,
I was hoping someone could help me out. I am new to VB.NET and
I don't know the language too well. I am trying to add an object
instance of a class to an array like in the code snipet below.
I run through the For Each loop 5 times and I place a reference
to the object in an ArrayList. Once I skip out of the loop block
I expect to have 5 different instances of that class in the
ArrayList. However, instead I have 5 instances of the class but
they are all of the last one that was added.
ObjList As ArrayList ' Declared at top of Form Class.
ObjList = New ArrayList ' In an initialize method.
' Code below is in some method in same Form Class.
Dim Info As InfoClass
Dim CrntObj as SomeClass
For Each Info in SomeArray ' 5 Info objects in SomeArray
CrntObj = myMgr.GetSomeClassObject(Info)
' do some stuff here
ObjList.Add(CrntObj)
Next sInfo
I know whats happening. I'm storing the reference to the
CrntObj variable in the ArrayList instead of what it references.
That's why I have 5 objects in the ArrayList all pointing to
the last item that was added. But why? I believe this would be
okay in C#. Can someone please help me out and tell me what I
am doing wrong?
Thanks,
Joe