473,413 Members | 1,733 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,413 software developers and data experts.

Why manipulating 1 object affects the other?

Ryu
I created 2 objects from a custom collection class.

IntCollection a = new IntCollection();
IntCollection b = new IntCollection();
a.FromInt32Array(someIntArray);
b = a;

However when ever I attempt to remove items from the object a, somehow b
will be affected. Is there a reason why this is happening?
Nov 16 '05 #1
6 1118
Reassigning a variable will cause the loss of whatever data the variable was
originally assigned unless there is another reference to that data.

// Assignment
IntCollection b = new IntCollection();
// Reassignment causes loss of original intCollection that was assigned to b
b = a;
// Now both b and a are variables that reference the intCollection first
assigned to a

"Ryu" <bl***************@yahoo.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
I created 2 objects from a custom collection class.

IntCollection a = new IntCollection();
IntCollection b = new IntCollection();
a.FromInt32Array(someIntArray);
b = a;

However when ever I attempt to remove items from the object a, somehow b
will be affected. Is there a reason why this is happening?

Nov 16 '05 #2
Ryu
So what should I do to prevent both variables from having the same
reference.
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
Reassigning a variable will cause the loss of whatever data the variable
was originally assigned unless there is another reference to that data.

// Assignment
IntCollection b = new IntCollection();
// Reassignment causes loss of original intCollection that was assigned to
b
b = a;
// Now both b and a are variables that reference the intCollection first
assigned to a

"Ryu" <bl***************@yahoo.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
I created 2 objects from a custom collection class.

IntCollection a = new IntCollection();
IntCollection b = new IntCollection();
a.FromInt32Array(someIntArray);
b = a;

However when ever I attempt to remove items from the object a, somehow b
will be affected. Is there a reason why this is happening?


Nov 16 '05 #3
Don't perform the assignment

a=b;

Is IntCollection a type you have defined?

And what are you trying to do, get a copy of a into b?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

So what should I do to prevent both variables from having the same
reference.

Nov 16 '05 #4
Ryu
Yes it is a custom type that I have defined. And Yes I am trying to get a
copy of a into b.
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uD**************@TK2MSFTNGP12.phx.gbl...
Don't perform the assignment

a=b;

Is IntCollection a type you have defined?

And what are you trying to do, get a copy of a into b?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

So what should I do to prevent both variables from having the same
reference.

Nov 16 '05 #5
Would b = a.Clone(); work? Or is the clone method not deep enough?

MattC
"Ryu" <bl***************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Yes it is a custom type that I have defined. And Yes I am trying to get a
copy of a into b.
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uD**************@TK2MSFTNGP12.phx.gbl...
Don't perform the assignment

a=b;

Is IntCollection a type you have defined?

And what are you trying to do, get a copy of a into b?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

So what should I do to prevent both variables from having the same
reference.


Nov 16 '05 #6
Reference types don't copy by default. You will have to add copy functionality to your IntCollection and call it. There is an interface called ICloneable which has a single method Clone on it - its defined like this

public interface ICloneable
{
object Clone();
}

You implement this interface to give your type copy semantics and would change your code to this

IntCollection a = new IntCollection();
IntCollection b = (IntCollection)a.Clone();

In your Clone method you would have something like this (I'm making some assumptions of how your IntCollection works from its name)

public object Clone()
{
IntCollection ret = new IntCollection();
foreach( int i in this.myIntStore)
{
ret.Add(i);
}
return ret;
}

One last thing is I remember reading some time ago that there were plans to obsolete IColneable in the next version but I haven't downloaded the latest build so I don't know if that still is the case. If it is you might simply want to add a public method, something like

public IntCollection Copy()
{
// details similar to above
}

and have your calling code as

IntCollection a = new IntCollection();
IntCollection b = a.Copy();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Yes it is a custom type that I have defined. And Yes I am trying to get a
copy of a into b.
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uD**************@TK2MSFTNGP12.phx.gbl...
Don't perform the assignment

a=b;

Is IntCollection a type you have defined?

And what are you trying to do, get a copy of a into b?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

So what should I do to prevent both variables from having the same
reference.


Nov 16 '05 #7

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

Similar topics

5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
3
by: John Salerno | last post by:
To test this out a wrote a little script as an exercise: for num in range(1, 10): x = 'c' * num y = 'c' * num if x is y: print 'x and y are the same object with', num, 'characters' else:...
2
by: Hymer | last post by:
Hello, I have a small two-column table with three rows. The first column has a logo and the second column has the name of the organization. The logo's in the first column are too high. That...
19
by: Mark Rae | last post by:
Hi, Is it possible to have programmatic access to the Page object in Application_BeginRequest, or is it too early in the lifecycle...? E.g. to be able to change a page's MasterPage...
1
by: Hoss | last post by:
Hello- I have the following Javascript code function Obj() { obj.squares = new Array(); } Obj.prototype.Load= function(xdoc) { var goat = "head"; xdoc.ProcessNodes("squares/square",...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
5
by: krishna.kishore.0987 | last post by:
Why are always object pointers used? (i.e., ObjectType *objPointer) what are the advantages of using object pointers Vs objects (ObjectType obj) one advantage I see is passing them across methods,...
1
by: Stephen Cattaneo | last post by:
Hi all, I am relatively new to socket programming. I am attempting to use raw sockets to spoof my IP address. From what I can tell I will have to build from the Ethernet layer on up. This is...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.