Re: Why does the instance change?



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" <guffa@xxxxxxxxx> wrote in message
news:u15x$2RbGHA.4580@xxxxxxxxxxxxxxxxxxxxxxx
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


.


Loading