Re: Instances effecting each other?
- From: "Lebesgue" <lebesgue@xxxxxxxxx>
- Date: Mon, 1 May 2006 23:20:07 +0200
No, it is not right.
C# doesn't have late binding (did you come from VB?) and Object doesn't have
any member called x or y, so this would not work.
//Please note that if x and y are reference types, you will want to clone
them as well.
public object Clone()
{
myObj instance = new myObj();
instance.x = this.x;
return instance;
}
myObj.x
Would the above return a clone not a reference?
it will definitely return a reference to a new object (to the clone). I'm
afraid you do not fully understand what an object and what a reference is...
"Daniel" <DanielV@xxxxxxxxxxxxxxxx> wrote in message
news:efpk2IWbGHA.3888@xxxxxxxxxxxxxxxxxxxxxxx
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@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 theNo, it isn't a different instance.
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 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: Fabio
- Re: Instances effecting each other?
- From: Daniel
- 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: Ok found a way of recreating my problem better
- Previous by thread: Re: Instances effecting each other?
- Next by thread: Re: Instances effecting each other?
- Index(es):
Relevant Pages
|