Below is working code from my application.

All I'm pulling back from the table is is IDX and an associated string.

ls is a List<T> of the same class tmpINC is an instance of.

When I test tmpINC before adding it to the list, the data in IDX and Name is correct.

However in the loop I do after (to check the data with the MessageBox.Show) every record is the same (the last record added).

What am I missing here.

// Fill the List ...
  if (ds.Tables[0].Rows.Count > 0)
  {
     for (int x = 0; x < ds.Tables[0].Rows.Count; x++)
     {
        tmpINC.IDX = int.Parse(ds.Tables[0].Rows[x][0].ToString());
        tmpINC.Name = ds.Tables[0].Rows[x][1].ToString();
        ls.Add(tmpINC);
     }

     for (int x = 0; x < ls.Count; x++ )
     {
        MessageBox.Show(ls[x].IDX.ToString() + "\n" + ls[x].Name);
     }
  }

Thanks for looking guys.

Recommended Answers

All 4 Replies

tmpInc is an object, and you are storing a reference to that object in the list (you are not making a copy of the object). So when you change the values in the object you get this affect. You need to create a new object each time:

for (int x = 0; ... blah ...) {
    tmpInc = new WhatEverClassItIs();
    tmpInc.IDX = int.Parse(ds.blah blah blah)
}
commented: ahh, the age old ref vs value type mistake :p +3

tmpInc is an object, and you are storing a reference to that object in the list (you are not making a copy of the object). So when you change the values in the object you get this affect. You need to create a new object each time:

for (int x = 0; ... blah ...) {
    tmpInc = new WhatEverClassItIs();
    tmpInc.IDX = int.Parse(ds.blah blah blah)
}

Ok, this works ... Momerath is correct (thank you Momerath). But now I need help understanding why?

On the surface, it would seem like what Momerath is saying is, a list is really just an array of pointers. But that can't be true, or the data would go away when each of the tmpINC objects went away.

I don't understand how, if it is a reference, I can toss the original object, but the data is still available to the list????

commented: always always always ask why :) +3

But that can't be true, or the data would go away when each of the tmpINC objects went away.

You have to understand the heap structure and the working of garbage collector. The actual data for reference types is stored in the heap. Both tempInc and list reference to it. When tempInc goes away, data is still there because there is already another variable which reference to it. When there is no reference to heap data, garbage collector frees that memory location. I hope you understand.

I found the article here very useful when i was trying to get my head around the value vs reference concept.

Essentially, you are correct about them being like pointers.
Lets break it down in code, see if it helps:

ReferenceClass myVariable; //create a space in memory to store reference
myVariable = new ReferenceClass(); //create a new object in memory

First you create a variable called myVariable which will store a memory address for the object you create.
Next you create the object itself in memory by calling its constructor. The memory address of this newly created object is stored in your variable.

So when you do something like this:

ReferenceType mySecondVariable = myVariable;

What you are actually doing is storing the same address in a second variable. Now both myVariable and mySecondVariable point to the same object in memory and any changes you make through one will be reflected in the other.
Now, when you pass myVariable as a parameter to a method; if you pass it by reference the method will reflect any changes back to myVariable which will in turn be acting on the object in memory. If you pass it by value, the method will receive a copy of the value stored in myVariable; the value stored is a reference to our object in memory. So the method is now acting directly on the object in memory. The end result is the same; any changes the method makes will be shown when accessing our original myVariable because throughout all of this we have only ever had a single instance of our ReferenceType class in memory.

Hope this helped some :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.