Re: Why does the instance change?



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


.



Relevant Pages

  • Why does the instance change?
    ... Object myObject = new Object; ... iteration i change the position vector, however when i do this and check my ... I presume that a new memory allocation isn't being made and so on setting ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does the instance change?
    ... on one iteration and on the next iteration immediately after the new ... I presume that a new memory allocation isn't being made and so on setting ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does the instance change?
    ... on one iteration and on the next iteration immediately after the new ... I presume that a new memory allocation isn't being made and so on setting ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does the instance change?
    ... on first iteration: ... steped through it in debug this was verified, the correct positon details ... I presume that a new memory allocation isn't being made and so on setting ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does the instance change?
    ... iteration in the loop? ... On my external class that calls the code withina loop. ... PlayingCard theCardSceneObj = new PlayingCard; ... I presume that a new memory allocation isn't being made and so on ...
    (microsoft.public.dotnet.languages.csharp)

Loading