Re: Why does the instance change?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Ok you are going to need to be clearer ... this code works fine.

class PositionVector

{

public int x, y, z;

public PositionVector(int _x, int _y, int _z)

{

x = _x;

y = _y;

z = _z;

}

public override string ToString()

{

return this.x.ToString() + ":" + this.y.ToString() + ":" +
this.z.ToString();

}

}

class myClass

{

public PositionVector PositionVector;

}

class Program

{

static void Main(string[] args)

{

List<myClass> myList = new List<myClass>();

for (int i = 0; i < 10; i++)

{

myClass myObject = new myClass();

myObject.PositionVector = new PositionVector(10 + (i * 3), 10 + (i * 5),
10);

myList.Add(myObject);

}

foreach (myClass c in myList)

{

Console.WriteLine(c.PositionVector.ToString());

}

}

}





"Daniel" <DanielV@xxxxxxxxxxxxxxxx> wrote in message
news:OgSFzlNbGHA.3812@xxxxxxxxxxxxxxxxxxxxxxx

Well thats just the point, it doesnt make sense to me either. The Object
bit i gave was just an example but an accurate one, here's a real one:

List<myClass> myList = new List<myClass>();

for(int i=0; i<10; i++)
{
myClass myObject = new Object();
myObject.PositionVector = new Vector3(10 + (i*3),10 + (i*5),10);

myList.Add(myObject);
}
And no nothing is static.

So as i said if were to debug and look at what is on myList it goes like
this

on first iteration:

myObject.Position = 10,10,10

on second iteration just before the Add myObject.Position = 13, 15, 10
then once added, myList now has 2 entries, but both have position
13,15,10.

So my question is, is there something about List that i don't know of to
explain how the first instance put on the list could be effected by the
second?

thanks


"Greg Young" <DruckDruckGoose@xxxxxxxxxxx> wrote in message
news:u032NYNbGHA.4424@xxxxxxxxxxxxxxxxxxxxxxx
I am not understanding some of your explanation ...

can you also post the definition of "Object" does it by chance have
positionvector declared as a static field ?

Cheers,

Greg


"Daniel" <DanielV@xxxxxxxxxxxxxxxx> wrote in message
news:OzQrARNbGHA.3828@xxxxxxxxxxxxxxxxxxxxxxx
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.PositionVector = new Vector3(10,10,10);

myList.Add(myObject);


With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate
entry but they all have the position of the last one to be added. When i
steped through it in debug this was verified, the correct positon
details are in on one iteration and on the next iteration immediately
after the new instance has its position changed so too does the one
already in my List.

I presume that a new memory allocation isn't being made and so on
setting position it overwrites the same memory my list is looking at but
why? I thought the 'new' operator explicitly tells it to create a new
memory allocation? Am i missing something? Anyone know how i can prevent
this situation.

Thanks







.



Relevant Pages

  • Re: Local variables within looping constructs
    ... > the iteration. ... variable like an int there may well be no cost at all. ... That isn't within the loop, though, that is exactly equivalent to ... variables local to where they are used inproves clarity and reduces the ...
    (comp.lang.c)
  • Re: Unsure about Something
    ... I don't think there is any other way to do this (declare a local ... >>In each iteration you have declared a new variable with the name "a". ... > int n = i; ... > is re-created on each iteration of the loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pointer difficulties
    ... Each iteration of the top loop results in populating ywith the j'th column ... two stores to an "element" one past the final element of an array. ... int main ... $ gcc -DVERS1 -o code code.c ...
    (comp.lang.c)
  • Re: Safest way to convert chars to ints
    ... | int main ... Hmn...not that I understand Objective-C, but the iteration ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Task Problem!!!!
    ... or at least a mix of iteration and ... recursion, like the following: ... size_t y, int clr, int bg_clr) ... Dig the even newer still, yet more improved, sig! ...
    (comp.lang.c)