473,398 Members | 2,525 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,398 software developers and data experts.

Copy instances

Hi,

I want to do a very simple thing that I use to do it in C++.

I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I want
to pass a copy of it.

IFoo MyInstanceOfFoo = new Foo();

IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}

Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)

Regards
--
Salvador
Jul 10 '06 #1
5 8730

Salvador wrote:
Hi,

I want to do a very simple thing that I use to do it in C++.

I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I want
to pass a copy of it.

IFoo MyInstanceOfFoo = new Foo();

IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}

Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)
You need to have your class / interface implement ICloneable, then call
MyInstanceOfFoo.Clone();

Jul 10 '06 #2
Check out
public class CloneHelpers

at this url:
http://www.15seconds.com/issue/041124.htm

or

public class CloneHelpers
{
public static object DeepClone(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return b.Deserialize(m);

}
public static bool DeepEquals(object objA,object objB)
{
MemoryStream serA = serializedStream(objA);
MemoryStream serB = serializedStream(objB);
if(serA.Length!=serA.Length)
return false;
while(serA.Position<serA.Length)
{
if(serA.ReadByte()!=serB.ReadByte())
return false;
}
return true;

}
public static MemoryStream serializedStream(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;

return m;
}
}


"Salvador" <Sa******@discussions.microsoft.comwrote in message
news:9D**********************************@microsof t.com...
Hi,

I want to do a very simple thing that I use to do it in C++.

I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I
want
to pass a copy of it.

IFoo MyInstanceOfFoo = new Foo();

IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}

Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)

Regards
--
Salvador

Jul 10 '06 #3
Thanks, that means that there is no way to pass reference object by value in
C#?

Implementing the Clone will force me to copy property by property....
--
Salvador

"Bruce Wood" wrote:
>
Salvador wrote:
Hi,

I want to do a very simple thing that I use to do it in C++.

I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I want
to pass a copy of it.

IFoo MyInstanceOfFoo = new Foo();

IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}

Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)

You need to have your class / interface implement ICloneable, then call
MyInstanceOfFoo.Clone();

Jul 10 '06 #4
Cool, Nice solution, thanks
--
Salvador

"sloan" wrote:
Check out
public class CloneHelpers

at this url:
http://www.15seconds.com/issue/041124.htm

or

public class CloneHelpers
{
public static object DeepClone(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return b.Deserialize(m);

}
public static bool DeepEquals(object objA,object objB)
{
MemoryStream serA = serializedStream(objA);
MemoryStream serB = serializedStream(objB);
if(serA.Length!=serA.Length)
return false;
while(serA.Position<serA.Length)
{
if(serA.ReadByte()!=serB.ReadByte())
return false;
}
return true;

}
public static MemoryStream serializedStream(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;

return m;
}
}


"Salvador" <Sa******@discussions.microsoft.comwrote in message
news:9D**********************************@microsof t.com...
Hi,

I want to do a very simple thing that I use to do it in C++.

I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I
want
to pass a copy of it.

IFoo MyInstanceOfFoo = new Foo();

IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}

Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)

Regards
--
Salvador


Jul 10 '06 #5

Salvador wrote:
Thanks, that means that there is no way to pass reference object by value in
C#?
That is correct.
Implementing the Clone will force me to copy property by property....
Check out the Object.MemberwiseClone() method. It still copies property
by property, but it saves you having to write the code yourself.

Jul 10 '06 #6

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...
6
by: amit.bhatia | last post by:
Hi, I have also posted this to the moderated group. I have 2 classes A and B, what does the following mean in header file for class A: class A { class B &b; .... };
4
by: KraftDiner | last post by:
I'm having trouble getting a copy of and object... (a deep copy) I'm writing a method that creates a mirror image of an object (on screen) In order to do this i need to get a copy of the object...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
11
by: dalu.gelu | last post by:
Hi, can anyone help me by writing a sample code of defining a copy constructor in a class having data member as an object of another class. for eg: class A{ int x; public: A(){ x=6;} };
9
by: hfinster | last post by:
Hello, could somebody please shed light on the following problem with g++ (4.03 and 3.3.6 as well)? Obviously, the copy constructor is not executed, if I assign the result of a function call to...
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...
5
by: jgscott | last post by:
I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list<Nodeas a class member. Node it itself a class that makes extensive...
4
by: csharpula csharp | last post by:
Hello, I would like to know how can I copy from BindingList<objto some other BindingList<obj>? I tried to pass one binding list to other via constractor but this is copying it by reference and I...
3
by: yoma | last post by:
python version 2.5 in module copy we all know that copy have two method: copy() and deepcopy(). and the explain is - A shallow copy constructs a new compound object and then (to the extent...
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
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...
0
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...
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...

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.