473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Why does the instance change?

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
May 1 '06 #1
18 1721
sb
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
"Daniel" <Da*****@vestryonline.com> wrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
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

May 1 '06 #2
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" <Da*****@vestryonline.com> wrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
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

May 1 '06 #3

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" <Dr*************@hotmail.com> wrote in message
news:u0**************@TK2MSFTNGP02.phx.gbl...
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" <Da*****@vestryonline.com> wrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
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


May 1 '06 #4
"Daniel" <Da*****@vestryonline.com> a écrit dans le message de news:
Og**************@TK2MSFTNGP04.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 1 '06 #5
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" <Da*****@vestryonline.com> wrote in message
news:Og**************@TK2MSFTNGP04.phx.gbl...

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" <Dr*************@hotmail.com> wrote in message
news:u0**************@TK2MSFTNGP02.phx.gbl...
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" <Da*****@vestryonline.com> wrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
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



May 1 '06 #6
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

May 1 '06 #7
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" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
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

May 1 '06 #8
Daniel <Da*****@vestryonline.com> wrote:
Ok my apologies here is the exact code,
<snip>
That any clearer?


Not a lot. You still haven't provided a short but complete example
which demonstrates the problem. Something we can compile and run.

See http://www.pobox.com/~skeet/csharp/complete.html

(You may well find the problem while trying to come up with a short but
complete example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 1 '06 #9
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel <Da*****@vestryonline.com> wrote:
Ok my apologies here is the exact code,


<snip>
That any clearer?


Not a lot. You still haven't provided a short but complete example
which demonstrates the problem. Something we can compile and run.

See http://www.pobox.com/~skeet/csharp/complete.html

(You may well find the problem while trying to come up with a short but
complete example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 1 '06 #10
Daniel <Da*****@vestryonline.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 1 '06 #11
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Daniel <Da*****@vestryonline.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 1 '06 #12
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?

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
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


May 1 '06 #13
"Daniel" <Da*****@vestryonline.com> a écrit dans le message de news:
eL**************@TK2MSFTNGP05.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 1 '06 #14
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
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


May 1 '06 #15
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
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


May 1 '06 #16
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" <Da*****@vestryonline.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
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

May 1 '06 #17
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" <le******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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" <Da*****@vestryonline.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
> 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


May 1 '06 #18
Daniel <Da*****@vestryonline.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 1 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: delraydog | last post by:
If you've used the Google Gmail spell checker you know that it 'changes' the text area to another type of control that lets them show hyperlinks for the incorrect words. How does Google change the...
1
by: Lance | last post by:
Hi All, I change the current directory in a proc, using CurDir(). The change persists across executions. I would like the CurDir() return value to be the same at the start of each execution. Any...
1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
6
by: Marc Robitaille | last post by:
Hello, Hello, I developed a UserControl. It has funny behavior. It is composed of three controls. A texbox, a combobox and a button. There are three properties to indicate the visibility of...
2
by: stef mientki | last post by:
hello, Why does Configparser change names to lowercase ? As Python is case sensitive (which btw I don't like at all ;-) but now when really need the casesensitivity, because it handles about...
5
by: nma | last post by:
Hi Why the code below does not change the color. I can only see the pointer change only but it does not change the color? Please help and Thnaks nma <td width="100" height="17"><a...
2
by: bryanleo | last post by:
We are trying to read data from a microcontroller and interface it through serial port. The output is then displayed in Python using Pyserial or the hyperterminal, the former is more important ...
1
by: bibhukdas | last post by:
Hi All, Just to elaborate on the issue. The requirement is to handle this in javascript 1. User clicks on a button 2. Change the cursor style (We are using document.body.style.cursor="wait") 3....
1
by: Joe Wyrwas | last post by:
My main program is a Multiple Document Interface (MDI). I create and show 2 or more child forms inside the MDI parent. If I left click on the child forms, the focus and Z-index will change...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.