473,320 Members | 2,193 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,320 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
Jul 17 '05 #1
2 11592
Almost everybody in this newsgroup is using VB6 or lower. While you may get
a stray answer to VB.NET questions here, you should ask them in newsgroups
devoted exclusively to .NET programming. Look for newsgroups with either the
word "dotnet" or "vsnet" in their name.

For the microsoft news server, try these newsgroups...

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
microsoft.public.vsnet.general

There are some others, but these should get you started.

Rick - MVP

"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

Jul 17 '05 #2
Great! Thanks for the info!

-- Joe
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<dY********************@comcast.com>...
Almost everybody in this newsgroup is using VB6 or lower. While you may get
a stray answer to VB.NET questions here, you should ask them in newsgroups
devoted exclusively to .NET programming. Look for newsgroups with either the
word "dotnet" or "vsnet" in their name.

For the microsoft news server, try these newsgroups...

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
microsoft.public.vsnet.general

There are some others, but these should get you started.

Rick - MVP

"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

Jul 17 '05 #3

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

Similar topics

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...
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...
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...
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.