Making a cloned or copied object...

C

cmelnick

I have a custom class that basically consists of two elements, a name
(String) and value (object). I am a bit confused on how to clone or
make a copy of an instance of my object. If I have:

public class MyItem {
private string name;
private object value;

public MyItem() {
}

public string Name { get{...} set{...} }
public object Value { get{...} set{...} }
}

how do I make sure that it is a true copy, with no cross references?
If I added a Clone method something like this:

public MyItem Clone() {
MyItem temp = new MyItem();
temp.Name = this.name;
temp.Value = this.value;
}

wouldn't the name and value refer to the original instances of name and
value? How would I truly copy objects?
 
G

Guest

You can use binary serialization to create a clone or your object that is
truly separate from your original object. You'll need to add the
[Serializable] attribute to your class but it's pretty simple code to
implement.

[Serializable]
public class MyItem : ICloneable
{
private string _name;
private object _value;

public MyItem()
{
}

public string Name { get{ return _name; } set{ _name = value;} }
public object Value { get{ return _value; } set{ _value = value;} }

public object Clone()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, this);
ms.Position = 0;
return formatter.Deserialize(ms);
}
}

HTH,
Jorge
 
G

Guest

I forgot to mention that I threw in the IClonable interface since the class
has a "Clone" method anyway.

Jorge L Matos said:
You can use binary serialization to create a clone or your object that is
truly separate from your original object. You'll need to add the
[Serializable] attribute to your class but it's pretty simple code to
implement.

[Serializable]
public class MyItem : ICloneable
{
private string _name;
private object _value;

public MyItem()
{
}

public string Name { get{ return _name; } set{ _name = value;} }
public object Value { get{ return _value; } set{ _value = value;} }

public object Clone()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, this);
ms.Position = 0;
return formatter.Deserialize(ms);
}
}

HTH,
Jorge

cmelnick said:
I have a custom class that basically consists of two elements, a name
(String) and value (object). I am a bit confused on how to clone or
make a copy of an instance of my object. If I have:

public class MyItem {
private string name;
private object value;

public MyItem() {
}

public string Name { get{...} set{...} }
public object Value { get{...} set{...} }
}

how do I make sure that it is a true copy, with no cross references?
If I added a Clone method something like this:

public MyItem Clone() {
MyItem temp = new MyItem();
temp.Name = this.name;
temp.Value = this.value;
}

wouldn't the name and value refer to the original instances of name and
value? How would I truly copy objects?
 
E

Erin Anderson

Jorge said:
You can use binary serialization to create a clone or your object that is
truly separate from your original object. You'll need to add the
[Serializable] attribute to your class but it's pretty simple code to
implement.

[Serializable]
public class MyItem : ICloneable
{
private string _name;
private object _value;

public MyItem()
{
}

public string Name { get{ return _name; } set{ _name = value;} }
public object Value { get{ return _value; } set{ _value = value;} }

public object Clone()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, this);
ms.Position = 0;
return formatter.Deserialize(ms);
}
}

HTH,
Jorge

Sorry to jump in here, but I wanted to ask... Is this the same as:

<Serializable()> _
Public Class clsCard

in VBNet2003?

I couldn't figure out the ICloneable item, it seemed to cause an error
in VBN2003.

Also having Option Strict turned on caused an error with the line
"return formatter.Deserialize(ms);" I think I took care of that, though.
 
E

Erin Anderson

Jorge said:
[Serializable]
public class MyItem : ICloneable
{ [...]
public object Clone()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, this);
ms.Position = 0;
return formatter.Deserialize(ms);
}
}

Is it impossible to close an object with Option Strict On?

I use the above serialization technique and get an error on the return
line. The error is:

Object Strict On disallows implicit conversions from 'System.Object' to
MyApp.MyClass

How can I avoid this build error? Thanks for any help!
 
J

Jon Skeet [C# MVP]

Erin Anderson said:
Is it impossible to close an object with Option Strict On?

I use the above serialization technique and get an error on the return
line. The error is:

Object Strict On disallows implicit conversions from 'System.Object' to
MyApp.MyClass

How can I avoid this build error? Thanks for any help!

Cast the result of the clone to the appropriate type. It's only
*implicit* conversions which are disallowed.
 
E

Erin Anderson

Jon said:
Cast the result of the clone to the appropriate type. It's only
*implicit* conversions which are disallowed.

I did a DirectCast, which seems to have helped. Thank you for the pointer!
 
G

Guest

Erin,

Try using DirectCast():

Dim myobj as MyApp.MyClass = DirectCast(myobj.Clone(), MyApp.MyClass)

Option Strict On - means that VB will force you to cast the return value
from the Clone() method from "Object" to the type you realy want.

--
Jorge L. Matos



Erin Anderson said:
Jorge said:
[Serializable]
public class MyItem : ICloneable
{ [...]
public object Clone()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, this);
ms.Position = 0;
return formatter.Deserialize(ms);
}
}

Is it impossible to close an object with Option Strict On?

I use the above serialization technique and get an error on the return
line. The error is:

Object Strict On disallows implicit conversions from 'System.Object' to
MyApp.MyClass

How can I avoid this build error? Thanks for any help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top