473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Adding an Object to an ArrayList

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
Nov 20 '05 #1
2 5840
Hi,

Try declaring the variable inside the for loop. That way you
arent using the same variable each time.

Dim Info As InfoClass

For Each Info in SomeArray ' 5 Info objects in SomeArray
Dim CrntObj as SomeClass
CrntObj = myMgr.GetSomeClassObject(Info)
' do some stuff here
ObjList.Add(CrntObj)
Next sInfo

Ken
--------------
"Anchor" <jm****@gtnetinc.com> wrote in message
news:fb**************************@posting.google.c om...
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

Nov 20 '05 #2
Hi,

Try declaring the variable inside the for loop. That way you
arent using the same variable each time.

Dim Info As InfoClass

For Each Info in SomeArray ' 5 Info objects in SomeArray
Dim CrntObj as SomeClass
CrntObj = myMgr.GetSomeClassObject(Info)
' do some stuff here
ObjList.Add(CrntObj)
Next sInfo

Ken
--------------
"Anchor" <jm****@gtnetinc.com> wrote in message
news:fb**************************@posting.google.c om...
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anchor | last post by:
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...
2
by: Vishal Somaiya | last post by:
Hello I am trying read from a xml file, pull the values in a object and then add the object to an ArrayList. I am using a 'while' loop to move through each node in the xml file and pulling the...
4
by: Gustaf Liljegren | last post by:
It doesn't seem to work. I get the error: System.NullReferenceException Object reference not set to an instance of an object. Here's the code: public ArrayList _rar; .... private void...
0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: NuB | last post by:
I need some help on this, I'm trying to add to an arrayList and its not working here is what i have public ArrayList carMake public ArrayList _CarMake { get { carMake; } set( carMake = value;...
1
by: San | last post by:
Hi, I'm using a Managed C++ project with MFC support. I have an ArrayList pointer object alValue (ArrayList* alValue) to this object i want to add all the elements present in Vector<double>...
2
by: Bob Hollness | last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the questions I may ask! I have a datagrid which I have populated from a database. It works great! I have added a column, via...
2
by: Tomek R. | last post by:
Hello ! I've got weird problem when adding new datagrid item. Here is the situation: my grid dgDeps is binded to DepartmentList arraylist, stored in Session between round-trips.. To add new...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.