Why does the instance change?

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

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
 
Although I think I understand what you're saying, I'd like to see the actual
code. Hopefully you can trim things down enough to post a short sample that
demonstrates the problem.

-sb
 
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
 
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
 
"Daniel" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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.

1. You have not shown the declaration of myClass.
2. Your example instantiates Object into a myClass reference

This code is not complete enough to see if you have declared PositionVector
as static, which is the most likely cause of your problem.

Could you post a *complete* but *brief* and *compiling* example.

Joanna
 
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());

}

}

}
 
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)
 
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y + (j
* 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?
 
This goes through one huge class, which calls another class which is
inherited down from two more. Its impossbile to show a complete example.
Hence my original shortened version that showed only the parts needed to
recreate my problem. Ok i will sort this one out for myself, thanks anyway.
 
Daniel said:
This goes through one huge class, which calls another class which is
inherited down from two more. Its impossbile to show a complete example.

No it's not. It just requires a little work. No-one's asking you to
post everything in your real classes - just enough for us to compile
and run a program which demonstrates the problem.
Hence my original shortened version that showed only the parts needed to
recreate my problem. Ok i will sort this one out for myself, thanks
anyway.

It's great to show a shortened version that only shows the parts needed
- but it *didn't* allow anyone to recreate the problem. You need to
post:

1) Enough that people can compile and run the program
2) Not too much: nothing that isn't required to show the problem
3) What you expect to happen and what actually happened
 
What does ObjectID.Cards contain? Will it be a different number for each
iteration in the loop? If it won't, then you will get the same instance
from the GetObject method.

The GetObject method never creates new objects, it only returns objects
from the list in the _objMan object. That is the main difference from
the example code you showed first.
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y + (j
* 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?

Göran Andersson said:
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)
 
Thanks Jon

unfortunately i really dont know how to on this one, usually when i post an
issue i provide all the information needed. I know its difficult to help
when someone gives not everything but the reason i am having a problem may
be due to how complex this piece of code that uses it is.

I would have to rewrite my code anyway to give a shortened version to pull
out the non-needed parts then post that. As someone earlier stated using my
short eample they found no problems so i guess the issue is elsewhere.
Either way its very strange and for such a simple requirement i was
surprised to have a problem. It's ok i will sort it, nothing worse than
someone asking for help and unable to give help to those trying to help.
Hopefully i will get it.
 
"Daniel" <[email protected]> a écrit dans le message de (e-mail address removed)...

| unfortunately i really dont know how to on this one, usually when i post
an
| issue i provide all the information needed. I know its difficult to help
| when someone gives not everything but the reason i am having a problem may
| be due to how complex this piece of code that uses it is.
|
| I would have to rewrite my code anyway to give a shortened version to pull
| out the non-needed parts then post that. As someone earlier stated using
my
| short eample they found no problems so i guess the issue is elsewhere.
| Either way its very strange and for such a simple requirement i was
| surprised to have a problem. It's ok i will sort it, nothing worse than
| someone asking for help and unable to give help to those trying to help.
| Hopefully i will get it.

All you have to post to make this understandable is the part of the
PlayingCard class that includes the Position property, as that is the
property that you state is always the same for all instances of the
PlayingCard class.

e.g.

public class PlayingCard
{
private Vector3 position;

public Vector3 Position
{
get { return position; }
set { position = value; }
}
}

Does this look anything like the property declared in your class ?

Joanna
 
Hi Goran

Yes it does because i get the object but i did this

PlayingCard pc = new PlayingCard();

pc = GetObject(objectId); etc etc

And i still had the same problem.

In response to the other question, yes thats how my position method looks.

Göran Andersson said:
What does ObjectID.Cards contain? Will it be a different number for each
iteration in the loop? If it won't, then you will get the same instance
from the GetObject method.

The GetObject method never creates new objects, it only returns objects
from the list in the _objMan object. That is the main difference from the
example code you showed first.
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y +
(j * 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?

Göran Andersson said:
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
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
 
SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);

chair.Position = GetSeatPosition(1);

chair.FrameNum = 1;

_objMan.AddToRenderStack(chair);

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition(3);

chair2.FrameNum = 2;

_objMan.AddToRenderStack(chair2);



Ok in the above code when i debug, at the line chair2.Position both
instances are equal in data as you would expect.

Now when i go to the final _objMan.AddToRenderStack line both the chair
instance and the chair2 instance framenumbers become 2. But i only changed
chair2's one so chair being a differenct instance shouldnt be effected
right?



The SceneChair chair2 = new SceneChair(); should have created a new memory
space and then when i changed chair2's framenum the memory allocated for
chair2 shoudl change while chair's remains the same. Am i right? So why do
they both change later on??? This is why when i add it to my stack they are
equal, as it is changing them as i said before while on the stack as if it
were the same instance.




Göran Andersson said:
What does ObjectID.Cards contain? Will it be a different number for each
iteration in the loop? If it won't, then you will get the same instance
from the GetObject method.

The GetObject method never creates new objects, it only returns objects
from the list in the _objMan object. That is the main difference from the
example code you showed first.
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y +
(j * 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?

Göran Andersson said:
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
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
 
First - this does not compile.
Second - we don't know the definition of GetSeatPosition, SceneChair, type
of _objMan, definition of its GetObject and AddToRenderStack method,
SceneChair class definition,...

How can we help you if WE can not recreate the problem? Jon Skeet has
already provided you advice how to structure your question, but I think he
did not send you the link:
http://www.yoda.arachsys.com/csharp/complete.html
Can you post a complete program (OK, the thing you have posted is short
enough, just make it complete) that demonstrates the problem?

Daniel said:
SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);

chair.Position = GetSeatPosition(1);

chair.FrameNum = 1;

_objMan.AddToRenderStack(chair);

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition(3);

chair2.FrameNum = 2;

_objMan.AddToRenderStack(chair2);



Ok in the above code when i debug, at the line chair2.Position both
instances are equal in data as you would expect.

Now when i go to the final _objMan.AddToRenderStack line both the chair
instance and the chair2 instance framenumbers become 2. But i only changed
chair2's one so chair being a differenct instance shouldnt be effected
right?



The SceneChair chair2 = new SceneChair(); should have created a new memory
space and then when i changed chair2's framenum the memory allocated for
chair2 shoudl change while chair's remains the same. Am i right? So why do
they both change later on??? This is why when i add it to my stack they
are equal, as it is changing them as i said before while on the stack as
if it were the same instance.




Göran Andersson said:
What does ObjectID.Cards contain? Will it be a different number for each
iteration in the loop? If it won't, then you will get the same instance
from the GetObject method.

The GetObject method never creates new objects, it only returns objects
from the list in the _objMan object. That is the main difference from the
example code you showed first.
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y +
(j * 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?

No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a
complete example that will actually show the problem you are talking
about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
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
 
As i said before i cannot do that.

First there is too much to send and second this code is protected and
copyrighted so i will not be pasting large segements on a newsgroup.

I thought maybe i had overlooked something simple and someone was going to
say 'ah your not understaing how the new operator works' or something like
that. So don't worry clearly this is not a simple thing to fix and requires
going through a lot of code to see why these instances are not acting as
they should.

Thanks for your help but unfortunately i cannot provide more code,
definitely not a compilable version.

Lebesgue said:
First - this does not compile.
Second - we don't know the definition of GetSeatPosition, SceneChair, type
of _objMan, definition of its GetObject and AddToRenderStack method,
SceneChair class definition,...

How can we help you if WE can not recreate the problem? Jon Skeet has
already provided you advice how to structure your question, but I think he
did not send you the link:
http://www.yoda.arachsys.com/csharp/complete.html
Can you post a complete program (OK, the thing you have posted is short
enough, just make it complete) that demonstrates the problem?

Daniel said:
SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);

chair.Position = GetSeatPosition(1);

chair.FrameNum = 1;

_objMan.AddToRenderStack(chair);

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition(3);

chair2.FrameNum = 2;

_objMan.AddToRenderStack(chair2);



Ok in the above code when i debug, at the line chair2.Position both
instances are equal in data as you would expect.

Now when i go to the final _objMan.AddToRenderStack line both the chair
instance and the chair2 instance framenumbers become 2. But i only
changed chair2's one so chair being a differenct instance shouldnt be
effected right?



The SceneChair chair2 = new SceneChair(); should have created a new
memory space and then when i changed chair2's framenum the memory
allocated for chair2 shoudl change while chair's remains the same. Am i
right? So why do they both change later on??? This is why when i add it
to my stack they are equal, as it is changing them as i said before while
on the stack as if it were the same instance.




Göran Andersson said:
What does ObjectID.Cards contain? Will it be a different number for each
iteration in the loop? If it won't, then you will get the same instance
from the GetObject method.

The GetObject method never creates new objects, it only returns objects
from the list in the _objMan object. That is the main difference from
the example code you showed first.

Daniel wrote:
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_objMan.GetObject((int)ObjectID.Cards);

theCardSceneObj.SetCardFrame(player.Hand[j]);

theCardSceneObj.FaceUp = player.Hand[j].ShowState;

theCardSceneObj.IsVisible = true;

theCardSceneObj.Position = new Vector3(seatPos.X + (j * 15), seatPos.Y
+ (j * 10), 0);

_objMan.AddToRenderList(theCardSceneObj);



Inside the_objMan instance:

class ObjectManager

{

List<SceneObject> _sceneObjList;

List<SceneObject> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObject>();

}

public void AddToRenderList(SceneObject so)
{
_renderList.Add(so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}


That any clearer?

No, sorry, the code you show is *not* an accurate example. If you
can't show the actual code you are using, you have to at least show a
complete example that will actually show the problem you are talking
about.

It's near to impossible to locate an error in a code by looking at
some other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
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
 
Daniel said:
SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);

chair.Position = GetSeatPosition(1);

chair.FrameNum = 1;

_objMan.AddToRenderStack(chair);

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition(3);

chair2.FrameNum = 2;

_objMan.AddToRenderStack(chair2);


Ok in the above code when i debug, at the line chair2.Position both
instances are equal in data as you would expect.

Now when i go to the final _objMan.AddToRenderStack line both the chair
instance and the chair2 instance framenumbers become 2. But i only changed
chair2's one so chair being a differenct instance shouldnt be effected
right?

No. You're creating a new object in the line

SceneChair chair2 = new SceneChair();

but then you're immediately making that object useless by doing:

chair2 = chair;

At that point, chair2 and chair both reference the same object, so when
you make changes to that object, you'll see the changes through either
of the variables.
 

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

Back
Top