Jump to content

  • Log In with Google      Sign In   
  • Create Account

Copying objects in C#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 ricekrispyw   Members   -  Reputation: 388

Like
0Likes
Like

Posted 05 October 2005 - 04:25 AM

Hey. Being a noob to C# (though not to programming), I need some help making a copy of an object. I have a new object, and I have a reference to an object. I'm looking to make a by-value (or memberwise, or whatever) copy of this object. Setting objA = objB, as we all know, just makes objA refer to the same object as objB, but I want to copy the object that objB is refering to and make objA refer to that object. I hope I've been clear. Thanks in advance.

The best way to predict the future is to invent it.

Sponsor:

#2 kSquared   Members   -  Reputation: 1356

Like
0Likes
Like

Posted 05 October 2005 - 04:41 AM

There's a couple of ways.

1.) If this is in the object's class definition, override the Clone() implementation to use the protected MemberwiseClone() method.

public class PlayerInformation : ICloneable
{
// ...

object ICloneable.Clone()
{
// make memberwise copy
return this.MemberwiseClone();
}
}


2.) If it's not or if you don't have control over the class definition, you'll have to use Clone(). If you don't know whether it's ICloneable or not, you'll have to test this first:

T t1 = new T();

// Make sure this can be cloned.
ICloneable original = t1 as ICloneable;

T copy = null;

if (original != null)
{
copy = original.Clone();
}

return copy;


If it's safe for you to assume that it's ICloneable, then you can just call Clone(), using:

T t1 = new T();
T t2 = t1.Clone();


or the more rigorous form,

T t1 = new T();
T t2 = ((ICloneable)t1).Clone();


Clone() returns a copy of the object but makes no guarantees about whether or not it's a deep copy or a shallow copy.

#3 Etherstar   Members   -  Reputation: 200

Like
0Likes
Like

Posted 05 October 2005 - 04:44 AM

Your real only solution is to either create a copy constructor or implementing a clone or copy method. I'm guessing the reasoning for not implementing copy constructors automatically is to limit boxing/unboxing, so just be sure that you actually need to make new objects.

If you don't have access to the class's definition (or interface definition if it has one) the above poster's solution is ideal. Otherwise I would make a custom copy constructor.

#4 ricekrispyw   Members   -  Reputation: 388

Like
0Likes
Like

Posted 05 October 2005 - 05:38 AM

OK. Thanks. I'll take a look at Clone(). I don't have control over the class definintion. (The class in question is actually KeyboardState from DirectInput.) Thanks again.

[Edited by - ricekrispyw on October 5, 2005 4:38:39 PM]

The best way to predict the future is to invent it.

#5 ricekrispyw   Members   -  Reputation: 388

Like
0Likes
Like

Posted 05 October 2005 - 10:40 AM

OK, the class (it wasn't Key, it was KeyboardState...) isn't IClonable. Is there no other way?

And if it's not, how do I convert an enum to an int? For example:
public enum Example
{
val1;
val2;
}


If I have an instance of Example that's, say, equal to val1, how do I convert that to an int?

Thanks a bunch and thanks for putting up with my C# newbishness.

The best way to predict the future is to invent it.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS