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

Copy of object

Hi all,

How can I create a method that returns a copy of an object, not a reference to the object itself.

Thanks !

Heinz
Jul 21 '05 #1
4 1664
Hi Heinz,

Depends on the class.
Normally you might want to use ICloneable.Clone method which might in turn
invoke protected MemberwiseClone method that creates a shallow copy.
Another way would be to serialize the object to a memory stream and
deserialize it - you'll end up with deep copy. Of course, all involved
classes will have to support serialization.
But ... it really depends on the class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Heinz" <he**********@posterjet.com> wrote in message
news:A1**********************************@microsof t.com...
Hi all,

How can I create a method that returns a copy of an object, not a reference to the object itself.
Thanks !

Heinz

Jul 21 '05 #2
Hi Heinz,

Depends on the class.
Normally you might want to use ICloneable.Clone method which might in turn
invoke protected MemberwiseClone method that creates a shallow copy.
Another way would be to serialize the object to a memory stream and
deserialize it - you'll end up with deep copy. Of course, all involved
classes will have to support serialization.
But ... it really depends on the class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Heinz" <he**********@posterjet.com> wrote in message
news:A1**********************************@microsof t.com...
Hi all,

How can I create a method that returns a copy of an object, not a reference to the object itself.
Thanks !

Heinz

Jul 21 '05 #3
I use the following method - accepts any object whose class is marked
'serializable'. If you want a shallow copy, which is what I usually
use, mark the object references of the class you're copying as
non-serializable and copy the references separately after calling
CloneObject:

internal static object CloneObject(object oObject)
{
//returns a separate object with all serializable
//properties set to those of oObject (the object class
//must have the "Serializable" attribute)
System.IO.MemoryStream oStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter
oFormatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
oFormatter.Serialize(oStream, oObject);
oStream.Seek(0, System.IO.SeekOrigin.Begin);
return oFormatter.Deserialize(oStream);
}

Jul 21 '05 #4
I use the following method - accepts any object whose class is marked
'serializable'. If you want a shallow copy, which is what I usually
use, mark the object references of the class you're copying as
non-serializable and copy the references separately after calling
CloneObject:

internal static object CloneObject(object oObject)
{
//returns a separate object with all serializable
//properties set to those of oObject (the object class
//must have the "Serializable" attribute)
System.IO.MemoryStream oStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter
oFormatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
oFormatter.Serialize(oStream, oObject);
oStream.Seek(0, System.IO.SeekOrigin.Begin);
return oFormatter.Deserialize(oStream);
}

Jul 21 '05 #5

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
2
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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?
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...

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.