Instances effecting each other?

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

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.

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?



The SceneChair chair2 = new SceneChair(); should have created a new memory
space 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??? 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.
 
Your problem is here:

chair2 = chair

from this moment, you are changing the chair instance. The object that
chair2 was referencing after line chair2 = new SceneChair() went away,
because the actual object has lost its last reference (chair2 was its only
reference).
 
Daniel said:
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.
 
"Daniel" <[email protected]> a écrit dans le message de %[email protected]...

| SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);
|
| chair.Position = GetSeatPosition(1);
|
| chair.FrameNum = 1;
|
| _objMan.AddToRenderStack(chair);
|
| SceneChair chair2 = new SceneChair();

Here you assign chair2 to a new instance of SceneChair

| chair2 = chair;

Here you are assigning chair2 to chair, which is the first instance.

So now you have two references to the original chair object and the second
one that was held in chair2 is garbage collected.

Joanna
 
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.

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?
 
Implement ICloneable interface and call Clone on the instance you want to
copy. Also make sure you will not be returning "this" reference in the Clone
method implementation ;-)

Daniel said:
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.

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?


Göran Andersson said:
Not only in data. They are the same instance. You are not copying the
data of the object, but the reference to the object.


No, it isn't a different instance.


Yes, it does. But you overwrite the reference to that new object, so it's
thrown away and never used.


Not later on. As there is only one instance, it changes instantly when
you change it.


That is because they *are* the same instance.
 
Daniel said:
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...
 
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 said:
Daniel said:
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...
 
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 said:
Daniel said:
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...
 
You don't need to use Activator, new will suffice for creating new instance

Daniel said:
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 said:
Daniel said:
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...
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.
 
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()

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 said:
Daniel said:
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...
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.
 
No, just using the constructor will do fine.
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 said:
Daniel said:
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...
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.
 
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 said:
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 said:
Daniel said:
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...
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.
 
When I look closer at your code, you have mixed up the class names. Here
is what it should look like:

public myObj Clone() {
myObj obj = new myObj();
obj.x = this.x;
obj.y = this.y;
return obj;
}

Göran Andersson said:
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()

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 said:
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...

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.
 
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it clear
when you were using ref and not. Hence my confusion. Got it now, thanks for
all your help guys.

Lebesgue said:
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 said:
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 said:
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...

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.
 
System.Object is something very well defined in CLR, my point was just to
help you keep that in mind.
My name is Lebesgue, not Lebasque, dear Danpil.

Daniel said:
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it clear
when you were using ref and not. Hence my confusion. Got it now, thanks
for all your help guys.

Lebesgue said:
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 said:
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?


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...

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.
 
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.

// chair is a reference to the thingy in the collection
// It is not a clone as you wish
SceneChair chair = (SceneChair)_objMan.GetObject((int)ObjectID.Seats);

// This modifies the thingy in the original collection
chair.Position = GetSeatPosition(1);
chair.FrameNum = 1;

// now the original thing is referenced by two collections
_objMan.AddToRenderStack(chair);


// TADA: you have created a brand new virgin Thingy
SceneChair chair2 = new SceneChair();

// WOOPS: You lost the reference to the brand new virgin thingy
// Instead you are pointing to the original thingy living on both
collections
chair2 = chair;

//WOOPS: modify the original thingy again
//and put another reference to it in the second collection
// That is one popular thingy
chair2.Position = GetSeatPosition(3);
chair2.FrameNum = 2;
_objMan.AddToRenderStack(chair2);
---------------------------------------------
Could you show me how to do this?
Other folks have shown you how to clone the thingy.
So you code should look like this

Thingy myThingy = Manager.GetCloneofThingy(thingyID);
myThingy.Changestuff();
Manager.AddToRenderStack(myThingy );

Thingy thingy2 = myThingy.Clone()
thingy2 .Changestuff();
Manager.AddToRenderStack(myThingy );

----
Instead of
Thingy thingy2 = myThingy.Clone()
you could do this
Thingy thingy2 = new Thingy(myThingy); // copy constructor - calls Clone
internally

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?

Nope, they are the same.

Try this

using System;
using System.Collections;

public class Foo
{
private int x;
public Foo(int x)
{
this.x = x;
}
public int X {get{return x;} set{x = value;}}

public static void Main(string[] args)
{
Foo myFoo = new Foo(0);
Foo[] array = new Foo[5];
for(int i = 0; i < array.Length; i++)
{
myFoo.X++;
array = myFoo;
}
foreach(Foo foo in array)
Console.WriteLine(foo.X);
myFoo.X = 19;
foreach(Foo foo in array)
Console.WriteLine(foo.X);
}
}

See if the output matches your expectations

Hope this helps
Bill
 
Daniel said:
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it
clear when you were using ref and not. Hence my confusion. Got it now,
thanks for all your help guys.

Just to be clear
The C++ equivalent to your code is this

// chair2 and chair are both pointers to a SceneChair
SceneChair* chair2 = new SceneChair();
chair2 = chair;

And that code is a memory leak

now if you had

SceneChair chair;
SceneChair chair2 = chair; // now we are copying

Bill
 
"Lebesgue" <[email protected]> ha scritto nel messaggio

//Please note that if x and y are reference types, you will want to clone
them as well.

You *may* want to clone them.
This is the problem of shallow vs deep copy.

I think that if you need massive cloning you made mistakes in your code
design.

Maybe the solution is just the use of a structure (value type: always
copied) instead of a class (reference type: never copied and need of manuale
cloning).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top