Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList problem 1

Status
Not open for further replies.

djam

Technical User
Nov 15, 2002
223
CA
When I add MyObject into a ArrayList, the last MyObject when added copies itself into all the MyObjects in the ArrayList. So everything has the value of the last MyObject. I don't know why it does this.

any suggestions...

thanks

" ahhh computers, how they made our lives much simpler ;) "
 
Code?

I'll try remote viewing.

I'll bet you are changing MyObject after you add it to the the Arraylist and then adding MyObject again. Adding to an arraylist just adds a reference to the object into the arraylist, not a copy.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
djam,
I agree with JohnYingling. Objects are reference types and object variables store object references. It might be that you're storing the object references in your array and all the references are pointing to the same object. Let us see some of your code to see if this is indeed the case.

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
When I add the last one into the ArrayList it changes everything...

ArrayList ar = new ArrayList();

string sqlTemp = "select * from ELoaderRef";
reader = seDB.GetSqlResults(sqlTemp);

while (reader.Read())
{
CrisSSHolder ch = new CrisSSHolder();
ch.CustomerKey = reader["customerKey"].ToString();
ch.OrderType = reader["orderType"].ToString();
ar.Add(ch);
}

" ahhh computers, how they made our lives much simpler ;) "
 
How would I not add the reference of the object into the ArrayList?

" ahhh computers, how they made our lives much simpler ;) "
 
ar.Add(ch);

When I add the last element to the array list, everything changes in the arraylist

" ahhh computers, how they made our lives much simpler ;) "
 
sorry sorry, it changes everytime I add a new object to the arraylist. The data was named very similar to each other except for the last one and I thought it only changed for the last item.

But everytime I add a new item to the arraylist, it changes everything in the arraylist. This is probably bc its using the reference of the same object. How can I fix it so that it uses a different reference?

thanks

" ahhh computers, how they made our lives much simpler ;) "
 
The fields CrisSSHolder has is

1) CustomerKey
2) OrderType

" ahhh computers, how they made our lives much simpler ;) "
 
oh darn!! found the error, I copied some code from a different class when I created CrisSSHolder and the fields were declared static... changed it to non-static and it works...

thanks for your help

" ahhh computers, how they made our lives much simpler ;) "
 
Code!!!
I can see the names of the properties in
CrisSSHolder ch = new CrisSSHolder();
ch.CustomerKey = reader["customerKey"].ToString();
ch.OrderType = reader["orderType"].ToString();
but that means nothing. We must see the code showing how and where the data for the property is actually stored inside the class. Take NOTHING for granted.

When something criminal happens to someone in a "family", at whom do the police first direct all their attention, absent other evidence to the contrary? Answer: other family members. Well right now, you and your code are the prime suspects, not some mysterious "one armed" man (i.e. Microsoft .Net Framework) who might have wandered by :)

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Ok,

I wrote the last reply before seeing your "Eureka". Remember this time...it will help shorten the time for discovering the next error. Your first thought should always be, "this error is so bizarre that it has to my fault." Sometimes it is not...but rarely.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top