Copy instances

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 said:
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();
 
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;
}
}
 
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....
 
Cool, Nice solution, thanks


--
Salvador

sloan said:
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 said:
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.
 
Back
Top