469,625 Members | 1,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Duplicate instance.

Hi everybody. I know everyone has said to me that this doesn't exist but I'm
going to try anyway.

I have an instance of ClassA and I want to duplicate that instance, creating
another one that is independient of the first one. Isn't there any automatic
way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards
Nov 17 '05 #1
7 3521
System.Object has the method MemberwiseClone(). This will create a shallow
copy and not a deep copy. This means that reference types will be shared
across the copies. Value types in contrary will be copied.

If you are familiar with the book "Design Patterns" written by the gang of
four you should take a look at the Prototype pattern.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi everybody. I know everyone has said to me that this doesn't exist but
I'm going to try anyway.

I have an instance of ClassA and I want to duplicate that instance,
creating another one that is independient of the first one. Isn't there
any automatic way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards

Nov 17 '05 #2
Gracias Gabriel. I see this topic is a little complex, as I've seen many
different positions about where/how long/why we should use IClonable
interface or not. Some people say it will be deprecated on framework 2.0, as
you never know if it returns a shallow copy or a deep copy. I thought there
would be a method like MemberwiseClone(), with the diference that it could
create a real copy of the reference types, creating new memory locations and
copying the values from the first one.

I see i have to implement that by myself.

Thanks anyway!

"Gabriel Lozano-MorŠn" <ga************@no-spam.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
System.Object has the method MemberwiseClone(). This will create a shallow
copy and not a deep copy. This means that reference types will be shared
across the copies. Value types in contrary will be copied.

If you are familiar with the book "Design Patterns" written by the gang of
four you should take a look at the Prototype pattern.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi everybody. I know everyone has said to me that this doesn't exist but
I'm going to try anyway.

I have an instance of ClassA and I want to duplicate that instance,
creating another one that is independient of the first one. Isn't there
any automatic way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards


Nov 17 '05 #3
One thing that you can do is to implement a copy constructor. Like:

public ClassA( ClassA classA )
{
// Create the copy object here
}

And later in your program's actual processing part:
....
ClassA originalClass = new ClassA();
....
ClassA copyClass = new ClassA( originalClass );
....

Daryush

"Ti√ęsto" wrote:
Hi everybody. I know everyone has said to me that this doesn't exist but I'm
going to try anyway.

I have an instance of ClassA and I want to duplicate that instance, creating
another one that is independient of the first one. Isn't there any automatic
way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards

Nov 17 '05 #4
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
Gracias Gabriel. I see this topic is a little complex, as I've seen many
different positions about where/how long/why we should use IClonable
interface or not. Some people say it will be deprecated on framework 2.0,
as you never know if it returns a shallow copy or a deep copy. I thought
there would be a method like MemberwiseClone(), with the diference that it
could create a real copy of the reference types, creating new memory
locations and copying the values from the first one.

I see i have to implement that by myself.

Thanks anyway!

"Gabriel Lozano-MorŠn" <ga************@no-spam.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
System.Object has the method MemberwiseClone(). This will create a
shallow copy and not a deep copy. This means that reference types will be
shared across the copies. Value types in contrary will be copied.

If you are familiar with the book "Design Patterns" written by the gang
of four you should take a look at the Prototype pattern.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi everybody. I know everyone has said to me that this doesn't exist but
I'm going to try anyway.

I have an instance of ClassA and I want to duplicate that instance,
creating another one that is independient of the first one. Isn't there
any automatic way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards



Nov 17 '05 #5
The main problem, I see, is to create "indepependent copies" of the
reference types.

I imagine a recursive method that, given an instance of a class, will create
a copy of it and all of its value type values. But when it finds a reference
type, it should run this method recursively to create a new copy, and so,
and so...

BUT. (and now I think I'm really understanding the purpose of ICloneable) we
couldn't do this beacause not all classes have a default argument-less
constructor. So, we couldn't know in advance HOW the instance is to be
created. And as the "new" keyword is mandatory in order to allocate new
memory for the new instance (and thus creating independent instances) .

In an ideal escenario, all the classes would have a Clone method, which
creates a shallow copy and calls Clone method of every reference type, or
manages to do the same with strings, which are also ref types.

I'm just making questions to myself. Feel free to post your comments.
Regards...
"Gabriel Lozano-MorŠn" <ga************@no-spam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
Gracias Gabriel. I see this topic is a little complex, as I've seen many
different positions about where/how long/why we should use IClonable
interface or not. Some people say it will be deprecated on framework 2.0,
as you never know if it returns a shallow copy or a deep copy. I thought
there would be a method like MemberwiseClone(), with the diference that
it could create a real copy of the reference types, creating new memory
locations and copying the values from the first one.

I see i have to implement that by myself.

Thanks anyway!

"Gabriel Lozano-MorŠn" <ga************@no-spam.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
System.Object has the method MemberwiseClone(). This will create a
shallow copy and not a deep copy. This means that reference types will
be shared across the copies. Value types in contrary will be copied.

If you are familiar with the book "Design Patterns" written by the gang
of four you should take a look at the Prototype pattern.

Gabriel Lozano-MorŠn
Software Engineer
Sogeti

"TiŽsto" <a@a.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi everybody. I know everyone has said to me that this doesn't exist
but I'm going to try anyway.

I have an instance of ClassA and I want to duplicate that instance,
creating another one that is independient of the first one. Isn't there
any automatic way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards



Nov 17 '05 #6
In message <#F**************@TK2MSFTNGP14.phx.gbl>, Gabriel Lozano-MorŠn
<ga************@no-spam.com> writes
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.


<Shameful confession> I've done that before now. It works. </>

--
Steve Walker
Nov 17 '05 #7
The difficulty is one of semantics and domain meaning. I, too,
struggled with this problem a while back and never came to a
satisfactory conclusion.

The only conclusion I did come to is that there is no "blanket
solution" that fits all scenarios. For some objects in my problem
domain, a shallow copy is exactly what I want. For others, a deep copy
is what I want. For still others, they need something in between.

For example, if an object holds a reference to an immutable reference
type (that is, a class that has been designed so that once constructed
it can't be changed), then there's no sense in copying it, since (apart
from threading considerations) two objects holding a reference to the
same object causes no surprises.

On the other hand, making a shallow copy of a tree structure is just
plain useless.

Even for a single class, sometimes a shallow copy makes sense, and in
other situations in my code I need a deep copy.

I gave up.

(By the way, Equals() suffers from the same malaise: when are two
objects really equal? What does that mean? Same bugbear in a different
disguise.)

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Lowell Kirsh | last post: by
1 post views Thread by JStrummer | last post: by
6 posts views Thread by Arthur Dent | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.