473,789 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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 1295
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*****@vestry online.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
My previous thread got very large so here is my point again, but a better
example of my problem:

SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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*****@vestry online.com> a écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 4.phx.gbl...

| SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
|
| chair.Position = GetSeatPosition (1);
|
| chair.FrameNum = 1;
|
| _objMan.AddToRe nderStack(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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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*****@vestry online.com> wrote in message
news:Or******** ********@TK2MSF TNGP05.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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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.Creat eInstance(this. GetType());

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

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.AddToRe nderStack 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*****@vestry online.com> wrote in message
news:eM******** ********@TK2MSF TNGP02.phx.gbl. ..
Sorry following on from last example.....do i need to do this to make th
enew insatce?

object newObject = Activator.Creat eInstance(this. GetType());

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.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)_ob jMan.GetObject( (int)ObjectID.S eats);
>
> chair.Position = GetSeatPosition (1);
>
> chair.FrameNum = 1;
>
> _objMan.AddToRe nderStack(chair );
>
> SceneChair chair2 = new SceneChair();
>
> chair2 = chair;
>
> chair2.Position = GetSeatPosition (3);
>
> chair2.FrameNum = 2;
>
> _objMan.AddToRe nderStack(chair 2);
>
>
>
> 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.AddToRe nderStack 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

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

Similar topics

4
3853
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 learned today that I need to have a 64-bit instance. So, can I create a 64-bit instance "next to" the 32-bit instance I've got running now or must I nuke my 32-bit instance and then create a 64-bit instance? Thanks in advance for your help!
4
3149
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
12203
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 http://www.sqldev.net/misc/ListSQLSvr.htm I run all of them on a Windows XP Professional 64bit machine which has 2 instances: mymachine\SQLExpress (Express Edition) and MSSQLSERVER (Developer Edition (64bit)) SQL Browser Service: not running
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.