473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Instances effecting each other?

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.
May 1 '06 #1
18 1273
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" <Da*****@vestryonline.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.

May 1 '06 #2
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.
May 1 '06 #3
"Daniel" <Da*****@vestryonline.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP04.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 1 '06 #4
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

May 1 '06 #5
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" <Da*****@vestryonline.com> wrote in message
news:Or****************@TK2MSFTNGP05.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.


May 1 '06 #6
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.


May 1 '06 #7
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.


May 1 '06 #8
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.


May 1 '06 #9
You don't need to use Activator, new will suffice for creating new instance

"Daniel" <Da*****@vestryonline.com> wrote in message
news:eM****************@TK2MSFTNGP02.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

May 1 '06 #10
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

May 1 '06 #11
No, just using the constructor will do fine.

Daniel wrote:
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

May 1 '06 #12
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" <Da*****@vestryonline.com> wrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl... 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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

May 1 '06 #13
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" <le******@gmail.com> wrote in message
news:OO**************@TK2MSFTNGP04.phx.gbl...
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" <Da*****@vestryonline.com> wrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
> 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.


May 1 '06 #14
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 wrote:
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
> 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.

May 1 '06 #15
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" <Da*****@vestryonline.com> wrote in message
news:Oe**************@TK2MSFTNGP04.phx.gbl...
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" <le******@gmail.com> wrote in message
news:OO**************@TK2MSFTNGP04.phx.gbl...
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" <Da*****@vestryonline.com> wrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
> news:%2****************@TK2MSFTNGP05.phx.gbl...
>> 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.
>



May 1 '06 #16
"Daniel" <Da*****@vestryonline.com> wrote in message
news:Or****************@TK2MSFTNGP05.phx.gbl...
<snip>
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[i] = 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

May 2 '06 #17
"Daniel" <Da*****@vestryonline.com> wrote in message
news:Oe**************@TK2MSFTNGP04.phx.gbl...
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
May 2 '06 #18
"Lebesgue" <le******@gmail.com> ha scritto nel messaggio
news:OO**************@TK2MSFTNGP04.phx.gbl...

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

Free .Net Reporting Tool - http://www.neodatatype.net
May 2 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: tommydkat | last post by:
Well, I've finally gotten UDB 8.2 FixPak 3 up and running on my HP-UX 11i system, thanks to Mr McBride and IBM support. :) I created a 32-bit instance and that's running just fine. However, I...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
8
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to get a list of SQL Server Instances thru a VB.NET application. I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.