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

Best way to assign one class to another

Hello,

Suppose I have created 2 objects of MyClass:

MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();

I also defined variables inside MyClass1:

myClass1.myVar1 = 1 ;
myClass1.myVar2 = 2 ;

What would be the best, elegant, way to achive:

myClass2 = myClass1 ;

Thanks
Eitan

Jul 2 '07 #1
2 1835
On Jul 2, 4:34 pm, Eitan <E...@discussions.microsoft.comwrote:
Hello,

Suppose I have created 2 objects of MyClass:

MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();

I also defined variables inside MyClass1:

myClass1.myVar1 = 1 ;
myClass1.myVar2 = 2 ;

What would be the best, elegant, way to achive:

myClass2 = myClass1 ;

Thanks
Eitan
What you just did will work.

This would work if you want to clone it:
myClass2 = (MyClass)myClass1.Clone();

Jul 2 '07 #2
Hi,
"Eitan" <Ei***@discussions.microsoft.comwrote in message
news:84**********************************@microsof t.com...
Hello,

Suppose I have created 2 objects of MyClass:

MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();

I also defined variables inside MyClass1:

myClass1.myVar1 = 1 ;
myClass1.myVar2 = 2 ;

What would be the best, elegant, way to achive:

myClass2 = myClass1 ;
You have to clarify what you want, in the above code you still have two
instances and they will contain different values, but now you have both
myClass2 and myClass1 referencing to the same instance.
If what you want is making both instances to hold the same values you could
create a method to "copy" the values from one to the other. Of course this
can be more complex like what happen in the case that one member is a
reference type. Do you want to create another instance of it? or just copy
the current one?

Google for "shallow copy" , "deep copy" and take a look at the IClone
interface.
Jul 2 '07 #3

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

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
7
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
6
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.