473,405 Members | 2,154 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,405 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 5847
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.