Re: Why does the instance change?
- From: "Greg Young" <DruckDruckGoose@xxxxxxxxxxx>
- Date: Mon, 1 May 2006 01:02:54 -0400
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
.
- References:
- Why does the instance change?
- From: Daniel
- Re: Why does the instance change?
- From: Greg Young
- Re: Why does the instance change?
- From: Daniel
- Why does the instance change?
- Prev by Date: Re: Why does the instance change?
- Next by Date: [VS2005] Insert for DetailsView control
- Previous by thread: Re: Why does the instance change?
- Next by thread: Re: Why does the instance change?
- Index(es):
Relevant Pages
|