ArrayList

C

C#Newbie

Hi everyone, I'm new in this group and I have a question that might
have been answered already. I created an arraylist that I want to
contain the object that I created. But when I try to store these
objects, I end up having the same information in the arraylist. Here is
my code

in Person.cs

class PersonClass
{

string Name;
string Phone;


public PersonClass()
{
}

public PersonClass(string name, string phone)
{
Name = name;
Phone = phone;
}


}


in Call.cs
using Person;

class CallClass
{
ArrayList al = new ArrayList();

public CallClass()
{
DataSet personDataSet = new DataSet();
personDataSet = getAllPersons(); // function call to another
dll that I created

foreach(DataRow row in personDataSet.Tables[0].Rows)
{
al.add(new PersonClass(row[0].ToString(),
row[1].ToString());
}
}

}

So when I ran this program, my arraylist has the same information. What
is happening is that the new information is overwritting the object
previously stored. Any help would be appreciated. Thanks
 
M

Marina Levit [MVP]

1) Are you sure getAllPersons is retrieving the kind of data you think it
is?
2) How are you checking the arraylist to see what is in it?
 
C

C#Newbie

Marina said:
1) Are you sure getAllPersons is retrieving the kind of data you think it
is?
2) How are you checking the arraylist to see what is in it?

C#Newbie said:
Hi everyone, I'm new in this group and I have a question that might
have been answered already. I created an arraylist that I want to
contain the object that I created. But when I try to store these
objects, I end up having the same information in the arraylist. Here is
my code

in Person.cs

class PersonClass
{

string Name;
string Phone;


public PersonClass()
{
}

public PersonClass(string name, string phone)
{
Name = name;
Phone = phone;
}


}


in Call.cs
using Person;

class CallClass
{
ArrayList al = new ArrayList();

public CallClass()
{
DataSet personDataSet = new DataSet();
personDataSet = getAllPersons(); // function call to another
dll that I created

foreach(DataRow row in personDataSet.Tables[0].Rows)
{
al.add(new PersonClass(row[0].ToString(),
row[1].ToString());
}
}

}

So when I ran this program, my arraylist has the same information. What
is happening is that the new information is overwritting the object
previously stored. Any help would be appreciated. Thanks



1) I am sure that I am getting the right information.

2) I use breakpoints and I have output what is in the row using
messagebox and it works.
 
M

Marina Levit [MVP]

You didn't answer my second question. How are you checking what is in the
arraylist?

C#Newbie said:
1) Are you sure getAllPersons is retrieving the kind of data you think it
is?
2) How are you checking the arraylist to see what is in it?

C#Newbie said:
Hi everyone, I'm new in this group and I have a question that might
have been answered already. I created an arraylist that I want to
contain the object that I created. But when I try to store these
objects, I end up having the same information in the arraylist. Here is
my code

in Person.cs

class PersonClass
{

string Name;
string Phone;


public PersonClass()
{
}

public PersonClass(string name, string phone)
{
Name = name;
Phone = phone;
}


}


in Call.cs
using Person;

class CallClass
{
ArrayList al = new ArrayList();

public CallClass()
{
DataSet personDataSet = new DataSet();
personDataSet = getAllPersons(); // function call to another
dll that I created

foreach(DataRow row in personDataSet.Tables[0].Rows)
{
al.add(new PersonClass(row[0].ToString(),
row[1].ToString());
}
}

}

So when I ran this program, my arraylist has the same information. What
is happening is that the new information is overwritting the object
previously stored. Any help would be appreciated. Thanks



1) I am sure that I am getting the right information.

2) I use breakpoints and I have output what is in the row using
messagebox and it works.
 
C

C#Newbie

I did answer your second question. I inserted a breakpoint at foreach
and I went through the code one by one. Since I am using MS VC#, I am
able to see what is being stored in the ArrayList. One thing is for
sure. My person object is stored but when I declare a new person and
store the data, the object before that is being overwritten.
 
J

Jon Skeet [C# MVP]

C#Newbie said:
Hi everyone, I'm new in this group and I have a question that might
have been answered already. I created an arraylist that I want to
contain the object that I created. But when I try to store these
objects, I end up having the same information in the arraylist. Here is
my code

Could you post a short but *complete* program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

This is a common problem if you don't create a new object for every
item, but try to use the same object and filling it with new data. The
code that you have shown doesn't have this problem, though, or any other
signs of sharing data between objects.

Are you sure that you have posted the exact code that you used when you
saw the problem? No changes?
 
C

C#Newbie

For all those who reply, thank you for your help. With much more search
over the internet, I came across the topic reference type and value
type. The solution for this problem, for anyone who is searching is
that ArrayList cannot contain classes because they are of reference
type. The objects in the ArrayList must be of type value, or of type
struct. That solved my problem. I probably should have known this if I
read from the beginning of some of my books to the end and not just
learning by jumping in the code part right away.
 
J

Jon Skeet [C# MVP]

C#Newbie said:
For all those who reply, thank you for your help. With much more search
over the internet, I came across the topic reference type and value
type. The solution for this problem, for anyone who is searching is
that ArrayList cannot contain classes because they are of reference
type. The objects in the ArrayList must be of type value, or of type
struct. That solved my problem. I probably should have known this if I
read from the beginning of some of my books to the end and not just
learning by jumping in the code part right away.

No, that's not true at all. ArrayLists most certainly *can* and usually
*do* contain reference types. In fact, they can *only* contain
reference types - if you add a value type value to an ArrayList, it
will be automatically boxed into the reference type box for that value
type.
 
B

Bob Jones

No, that's not true at all. ArrayLists most certainly *can* and usually
*do* contain reference types. In fact, they can *only* contain
reference types - if you add a value type value to an ArrayList, it
will be automatically boxed into the reference type box for that value
type.

There is a distinct difference between this:
myArrayList.add(new Person(....));
and
Person Me = new Person ();
myArrayList.add(Me);

In the first case you're adding the object. In the second, you're
adding a reference to the object. I'd bet a Dunkin' Donut and coffee
that the second thing will work.
 
J

Jon Skeet [C# MVP]

Bob Jones said:
There is a distinct difference between this:
myArrayList.add(new Person(....));
and
Person Me = new Person ();
myArrayList.add(Me);

In the first case you're adding the object. In the second, you're
adding a reference to the object. I'd bet a Dunkin' Donut and coffee
that the second thing will work.

No, those two do *exactly* the same thing. They both add a reference.
The only difference is that in the second version you've still got a
local variable referring to the object as well. If you change the data
within the object via the local variable, those changes will be visible
through the ArrayList too.
 
T

Truong Hong Thi

This code snippet has no problem. And your explaination about
reference/value type is not relevent here.

If I should make a guess, I think, in your real code, you instantiate a
new ArrayList inside the loop. That way, the array list always contains
only one element.

Thi
 
T

Truong Hong Thi

As I know it, ArrayList always contains references.
Event if you add a value-type instance, it will be boxed and the
reference is stored.
There is no difference between the two except the one Jon said.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top