Re: Instances effecting each other?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Sorry following on from last example.....do i need to do this to make th
enew insatce?

object newObject = Activator.CreateInstance(this.GetType());



"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:uNVoXHWbGHA.4112@xxxxxxxxxxxxxxxxxxxxxxx
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what is
happening anyway.

So in that case how can i take an instance in an array and create a new
instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.

As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.

I always thought by using the new operator it solved this by allocating
new memory i didnt realise that the '=' sign copied the reference and not
the actual data.

Could you show me how to do this? What i dont understand is i do this
kind of thing all the time, the only difference here is that i am using
List instead of array, i preumse arrays create new instances and lists
use the reference?

No. Instances are never created automatically. You must have been lucky in
the past. That, or you have a lot of potential bugs lurking in your
code...

"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:%23jgbt4VbGHA.3364@xxxxxxxxxxxxxxxxxxxxxxx
Daniel wrote:
My previous thread got very large so here is my point again, but a
better example of my problem:

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.
Not only in data. They are the same instance. You are not copying the
data of the object, but the reference to the object.

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, it isn't a different instance.

The SceneChair chair2 = new SceneChair(); should have created a new
memory
space
Yes, it does. But you overwrite the reference to that new object, so
it's thrown away and never used.

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???
Not later on. As there is only one instance, it changes instantly when
you change it.

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.
That is because they *are* the same instance.


.



Relevant Pages

  • Re: Instances effecting each other?
    ... So in that case how can i take an instance in an array and create a new ... memory i didnt realise that the '=' sign copied the reference and not the ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instances effecting each other?
    ... What i dont understand is i do this kind of thing all the time, the only difference here is that i am using List instead of array, i preumse arrays create new instances and lists use the reference? ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instances effecting each other?
    ... So in that case how can i take an instance in an array and create a new ... use the reference? ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instances effecting each other?
    ... What i dont understand is i do this kind of thing all the time, the only difference here is that i am using List instead of array, i preumse arrays create new instances and lists use the reference? ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instances effecting each other?
    ... Would the above return a clone not a reference? ... So in that case how can i take an instance in an array and create a new ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)