Re: Instances effecting each other?
- From: "Daniel" <DanielV@xxxxxxxxxxxxxxxx>
- Date: Mon, 1 May 2006 22:00:36 +0100
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 aNot only in data. They are the same instance. You are not copying the
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.
data of the object, but the reference to the object.
Now when i go to the final _objMan.AddToRenderStack line both the chairNo, it isn't a different instance.
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 newYes, it does. But you overwrite the reference to that new object, so
memory
space
it's thrown away and never used.
and then when i changed chair2's framenum the memory allocated forNot later on. As there is only one instance, it changes instantly when
chair2 shoudl change while chair's remains the same. Am i right? So why
do
they both change later on???
you change it.
This is why when i add it to my stack they areThat is because they *are* the same instance.
equal, as it is changing them as i said before while on the stack as if
it
were the same instance.
.
- Follow-Ups:
- Re: Instances effecting each other?
- From: Göran Andersson
- Re: Instances effecting each other?
- From: Lebesgue
- Re: Instances effecting each other?
- References:
- Instances effecting each other?
- From: Daniel
- Re: Instances effecting each other?
- From: Göran Andersson
- Re: Instances effecting each other?
- From: Daniel
- Re: Instances effecting each other?
- From: Göran Andersson
- Instances effecting each other?
- Prev by Date: Re: Instances effecting each other?
- Next by Date: Re: Instances effecting each other?
- Previous by thread: Re: Instances effecting each other?
- Next by thread: Re: Instances effecting each other?
- Index(es):
Relevant Pages
|