Re: Instances effecting each other?
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Mon, 01 May 2006 23:16:36 +0200
Yes, if those are all the member variables you have in the class, that will work nicely.
You would declare the method as:
public myObj Clone()
Daniel wrote:
But now i am not sure how to create a new instance to do that with, i always thought 'new' did this..
Is this right?
Clone()
{
Object myObj = new myObj();
myObj.x = this.x;
myObj.y = this.y;
return myObj;
}
Would the above return a clone not a reference?
"Göran Andersson" <guffa@xxxxxxxxx> wrote in message news:uNVoXHWbGHA.4112@xxxxxxxxxxxxxxxxxxxxxxxDaniel wrote:Thats the answer i was looking for.As Lebesgue said, you should clone the object. Basically that means creating a new instance and copy all the data to it.
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.
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.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...
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?
"Göran Andersson" <guffa@xxxxxxxxx> wrote in message news:%23jgbt4VbGHA.3364@xxxxxxxxxxxxxxxxxxxxxxxDaniel wrote:My previous thread got very large so here is my point again, but a better example of my problem:Not only in data. They are the same instance. You are not copying the data of the object, but the reference to the object.
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 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 new memoryYes, it does. But you overwrite the reference to that new object, so it's thrown away and never used.
space
and then when i changed chair2's framenum the memory allocated forNot later on. As there is only one instance, it changes instantly when you change it.
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 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?
- 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
- Re: Instances effecting each other?
- From: Daniel
- 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
|