473,378 Members | 1,142 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,378 software developers and data experts.

How deep should a deep copy go? / Is an object that contains value type still a reference type?

Hi all,

I'm implementing the Clone() method through the ICloneable interface
and don't quite know how deep I need to go for a deep copy.

Example:

class A: ICloneable
{
object _val;

//Val will always be a value type
A(object val)
{
_val = val;
}

object Clone()
{
//Don't know if I this is sufficient
return new A(_val);

//Or do I have to go
object newVal;

if(val.GetType() == typeof(bool))
{
newVal = (bool) val;
}
//....etc....//

return new A(newVal)
}
}

Thus do I have to cast the object back to a value type before returning
the cloned instance.

So I suppose the real question is: Is an object that contains value
type still a reference type?

Thanks,
Andre

Nov 17 '05 #1
13 2148
Also,

If I need to cast the object back to value type, before creating the
new instance, is there a way of casting it automatically?

An example might make the question a bit clearer:

Instead of going:

public object CreateNewValueTypeObject(object obj)
{
object newObj;

if(obj.GetType() == typeof(bool))
{
newObj = (bool) obj;
}
else ....

}

Is it possible to do something like:

object newObj = (obj.GetType()) obj;

Thanks all,
Andre

Nov 17 '05 #2
Hi

To implement deep copy you can use serialization , thats how deep copy
is implemented. Shallow copy is done by reference , but deep copy means
create a complete new object in short real clooning.

Once we serialize the object all references are also serialzed.

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #3
Hi Shivprasad,

Thanks for your reply.

I did a search on searlization and came up with this:

http://weblogs.asp.net/whaggard/arch...3/02/3313.aspx

It looks pretty good. I think I'll give that a try.

Kind regards,
Andre

Nov 17 '05 #4
Andre,

There is no way to do what you are doing, but since you are assigning it
to an object, what's the point?

In .NET 2.0, you can use generics to provide a type-safe cast though
(you indicate the type parameter for the method).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ah****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Also,

If I need to cast the object back to value type, before creating the
new instance, is there a way of casting it automatically?

An example might make the question a bit clearer:

Instead of going:

public object CreateNewValueTypeObject(object obj)
{
object newObj;

if(obj.GetType() == typeof(bool))
{
newObj = (bool) obj;
}
else ....

}

Is it possible to do something like:

object newObj = (obj.GetType()) obj;

Thanks all,
Andre

Nov 17 '05 #5
Hi Nicholas,

Thanks for the reply.

I'm assigning it to an object because I want to be able to cater for
all value types and I don't want to create unique constructors, class
members, and class methods for each type of value type.

Also, would you mind having a look at my root/first post? (When
implementing the Clone() method; Do you know if I have to a) create a
new object and cast it to the relevant value type, or b) could I just
use the existing object (when the object contains value types only)?)

Many thanks,
Andre

Nov 17 '05 #6
Andre,

I've seen your first post, and quite frankly, it looks confusing. val
is a field in the class (it can't be passed in), so you know the type,
instantly. There is no reason to cast to an object and then back again.

If you are trying to write a template for all classes to perform a deep
copy, it would be better if you create a static helper method somewhere
which used reflection to do this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ah****@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
Hi Nicholas,

Thanks for the reply.

I'm assigning it to an object because I want to be able to cater for
all value types and I don't want to create unique constructors, class
members, and class methods for each type of value type.

Also, would you mind having a look at my root/first post? (When
implementing the Clone() method; Do you know if I have to a) create a
new object and cast it to the relevant value type, or b) could I just
use the existing object (when the object contains value types only)?)

Many thanks,
Andre

Nov 17 '05 #7
Replying to my own post. Quite sad.

Anyway, I found a way to create a create new object so that it'll be of
the same type as ANY other original object.

Confusing? Look at this bit of code:

public object CreateNewObject(object obj)
{
object newObject = Activator.CreateInstance( obj.GetType() );
}

You do however need a parameterless Constructor in the orginal class
(obj's class).
ah****@gmail.com wrote:
Also,

If I need to cast the object back to value type, before creating the
new instance, is there a way of casting it automatically?

An example might make the question a bit clearer:

Instead of going:

public object CreateNewValueTypeObject(object obj)
{
object newObj;

if(obj.GetType() == typeof(bool))
{
newObj = (bool) obj;
}
else ....

}

Is it possible to do something like:

object newObj = (obj.GetType()) obj;

Thanks all,
Andre


Nov 17 '05 #8
Replying to my own post. Quite sad.

Anyway, I found a way to create a create new object so that it'll be of
the same type as ANY other original object.

Confusing? Look at this bit of code:

public object CreateNewObject(object obj)
{
object newObject = Activator.CreateInstance( obj.GetType() );
}

You do however need a parameterless Constructor in the orginal class
(obj's class).
ah****@gmail.com wrote:
Also,

If I need to cast the object back to value type, before creating the
new instance, is there a way of casting it automatically?

An example might make the question a bit clearer:

Instead of going:

public object CreateNewValueTypeObject(object obj)
{
object newObj;

if(obj.GetType() == typeof(bool))
{
newObj = (bool) obj;
}
else ....

}

Is it possible to do something like:

object newObj = (obj.GetType()) obj;

Thanks all,
Andre


Nov 17 '05 #9
ah****@gmail.com wrote:
Hi all,

I'm implementing the Clone() method through the ICloneable interface

<snip>

Doing a "deep clone" is a serious matter and needs specific knowledge of
the target types, at least in a lot of scenarios. Cloning any type of
object is not that easy.

Let me give you an example.

Suppose you have the following classes:

- Order
- OrderLine

Their relationships are:

- An Order can hold references to many OrderLine objects
- An OrderLine object holds a reference to the Order that its a part of

Now...

If you want to clone an order, you would:

- Clone the Order
- Clone all the OrderLines attached to it and attach them to the cloned
order
- Fix up the references (so that the cloned OrderLine's reference to its
order points to the new, cloned, order)

If you want to clone an orderline, you would:

- Clone the orderline, leaving the reference to its order in place for
the new line (in other words, you duplicate a line which ends up
attached to the same order as the original)

This means that the process of cloning an orderline depends on wether
you are only cloning an orderline or wether you are cloning the whole order.

If you implement "deep cloning" in both without knowledge about where
you started you might end up with:

- Call .Clone on Order, produces new Order object and proceeds to call
..Clone on each OrderLine and attaching them to the new Order object
- For each .Clone call on each OrderLine, you clone that object, which
in turn clones the Order again, going back and produces a new Order
- Repeat ad nauseum

In other words, you can't write the cloning process for the orderline
without knowledge about the objects around it and what you are actually
cloning (where you started in other words).

Cloning is something you need to work into a class hierarchy as a whole
and you cannot do it separately for all the objects, or you will get a
broken implementation.

To add to the above problem, add a Customer to the whole thing. A Order
or OrderLine might contain a reference to the Customer, and possibly the
Customer might contain reference to all his orders. What happens if:

- You clone the order (leave it attached to the same customer)
- You clone the customer (clone all orders which will use a different
customer reference)

I've looked at several ways to implement cloning and have found that the
following three ways seems to work ok and leave little doubt about what
is actually happening:

1. Use a mechanism with attributes to tag properties in a class that
should be "fixed up" during cloning, in such a way that I can know what
to do depending on what kind of object the cloning process was started
from (ie. I can detail what happens if I clone a Customer vs. a Order
vs. a OrderLine). The attributes still need to be written so that they
cover each scenario (all the possible starting points for cloning)
2. Use a separate cloning helper class for the hierarchy with methods
that clearly detail what they do (CloneCustomer, CloneOrder,
CloneOrderLine) and leave the cloning mechanism completely out of the object
3. Give the Clone methods the type of the initial class I called .Clone
on so that they know what to clone and what to copy (this is just a
non-attribute version of #1).

The ICloneable interface gives you a lot of questions, many of which the
answer to is "ICloneable is not enough" so I've decided that I won't use
it for my own objects except for trivial scenarios (ie. objects which
clearly aren't meant to participate in big hierarchies and graphs).

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #10
ah****@gmail.com wrote:
Hi all,

I'm implementing the Clone() method through the ICloneable interface

<snip>

Doing a "deep clone" is a serious matter and needs specific knowledge of
the target types, at least in a lot of scenarios. Cloning any type of
object is not that easy.

Let me give you an example.

Suppose you have the following classes:

- Order
- OrderLine

Their relationships are:

- An Order can hold references to many OrderLine objects
- An OrderLine object holds a reference to the Order that its a part of

Now...

If you want to clone an order, you would:

- Clone the Order
- Clone all the OrderLines attached to it and attach them to the cloned
order
- Fix up the references (so that the cloned OrderLine's reference to its
order points to the new, cloned, order)

If you want to clone an orderline, you would:

- Clone the orderline, leaving the reference to its order in place for
the new line (in other words, you duplicate a line which ends up
attached to the same order as the original)

This means that the process of cloning an orderline depends on wether
you are only cloning an orderline or wether you are cloning the whole order.

If you implement "deep cloning" in both without knowledge about where
you started you might end up with:

- Call .Clone on Order, produces new Order object and proceeds to call
..Clone on each OrderLine and attaching them to the new Order object
- For each .Clone call on each OrderLine, you clone that object, which
in turn clones the Order again, going back and produces a new Order
- Repeat ad nauseum

In other words, you can't write the cloning process for the orderline
without knowledge about the objects around it and what you are actually
cloning (where you started in other words).

Cloning is something you need to work into a class hierarchy as a whole
and you cannot do it separately for all the objects, or you will get a
broken implementation.

To add to the above problem, add a Customer to the whole thing. A Order
or OrderLine might contain a reference to the Customer, and possibly the
Customer might contain reference to all his orders. What happens if:

- You clone the order (leave it attached to the same customer)
- You clone the customer (clone all orders which will use a different
customer reference)

I've looked at several ways to implement cloning and have found that the
following three ways seems to work ok and leave little doubt about what
is actually happening:

1. Use a mechanism with attributes to tag properties in a class that
should be "fixed up" during cloning, in such a way that I can know what
to do depending on what kind of object the cloning process was started
from (ie. I can detail what happens if I clone a Customer vs. a Order
vs. a OrderLine). The attributes still need to be written so that they
cover each scenario (all the possible starting points for cloning)
2. Use a separate cloning helper class for the hierarchy with methods
that clearly detail what they do (CloneCustomer, CloneOrder,
CloneOrderLine) and leave the cloning mechanism completely out of the object
3. Give the Clone methods the type of the initial class I called .Clone
on so that they know what to clone and what to copy (this is just a
non-attribute version of #1).

The ICloneable interface gives you a lot of questions, many of which the
answer to is "ICloneable is not enough" so I've decided that I won't use
it for my own objects except for trivial scenarios (ie. objects which
clearly aren't meant to participate in big hierarchies and graphs).

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #11
sh**********@yahoo.com wrote:
Hi

To implement deep copy you can use serialization , thats how deep copy
is implemented. Shallow copy is done by reference , but deep copy means
create a complete new object in short real clooning.

Once we serialize the object all references are also serialzed.

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/


Using serialization is good if you want to clone a whole object graph.
In some cases you only need to do a partial clone as far as the whole
graph is concerned (ie. clone an order attached to a customer, but let
it refer to the same customer as the original order). At that point you
will have some problems using serialization, which tends to do a
complete graph serialization, containing the customer and whatnot.

Of course, if you want to clone the complete object, including all
related objects, very little beat using serialization in terms of
simplicity of implementation.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #12
sh**********@yahoo.com wrote:
Hi

To implement deep copy you can use serialization , thats how deep copy
is implemented. Shallow copy is done by reference , but deep copy means
create a complete new object in short real clooning.

Once we serialize the object all references are also serialzed.

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/


Using serialization is good if you want to clone a whole object graph.
In some cases you only need to do a partial clone as far as the whole
graph is concerned (ie. clone an order attached to a customer, but let
it refer to the same customer as the original order). At that point you
will have some problems using serialization, which tends to do a
complete graph serialization, containing the customer and whatnot.

Of course, if you want to clone the complete object, including all
related objects, very little beat using serialization in terms of
simplicity of implementation.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #13
Hi Lasse,

Thanks for the post. It was really insightful.

I'll keep it in mind, next time I develop a more complex structure.

Many thanks,
Andre

Lasse Vågsæther Karlsen wrote:
ah****@gmail.com wrote:
Hi all,

I'm implementing the Clone() method through the ICloneable interface

<snip>

Doing a "deep clone" is a serious matter and needs specific knowledge of
the target types, at least in a lot of scenarios. Cloning any type of
object is not that easy.

Let me give you an example.

Suppose you have the following classes:

- Order
- OrderLine

Their relationships are:

- An Order can hold references to many OrderLine objects
- An OrderLine object holds a reference to the Order that its a part of

Now...

If you want to clone an order, you would:

- Clone the Order
- Clone all the OrderLines attached to it and attach them to the cloned
order
- Fix up the references (so that the cloned OrderLine's reference to its
order points to the new, cloned, order)

If you want to clone an orderline, you would:

- Clone the orderline, leaving the reference to its order in place for
the new line (in other words, you duplicate a line which ends up
attached to the same order as the original)

This means that the process of cloning an orderline depends on wether
you are only cloning an orderline or wether you are cloning the whole order.

If you implement "deep cloning" in both without knowledge about where
you started you might end up with:

- Call .Clone on Order, produces new Order object and proceeds to call
.Clone on each OrderLine and attaching them to the new Order object
- For each .Clone call on each OrderLine, you clone that object, which
in turn clones the Order again, going back and produces a new Order
- Repeat ad nauseum

In other words, you can't write the cloning process for the orderline
without knowledge about the objects around it and what you are actually
cloning (where you started in other words).

Cloning is something you need to work into a class hierarchy as a whole
and you cannot do it separately for all the objects, or you will get a
broken implementation.

To add to the above problem, add a Customer to the whole thing. A Order
or OrderLine might contain a reference to the Customer, and possibly the
Customer might contain reference to all his orders. What happens if:

- You clone the order (leave it attached to the same customer)
- You clone the customer (clone all orders which will use a different
customer reference)

I've looked at several ways to implement cloning and have found that the
following three ways seems to work ok and leave little doubt about what
is actually happening:

1. Use a mechanism with attributes to tag properties in a class that
should be "fixed up" during cloning, in such a way that I can know what
to do depending on what kind of object the cloning process was started
from (ie. I can detail what happens if I clone a Customer vs. a Order
vs. a OrderLine). The attributes still need to be written so that they
cover each scenario (all the possible starting points for cloning)
2. Use a separate cloning helper class for the hierarchy with methods
that clearly detail what they do (CloneCustomer, CloneOrder,
CloneOrderLine) and leave the cloning mechanism completely out of the object
3. Give the Clone methods the type of the initial class I called .Clone
on so that they know what to clone and what to copy (this is just a
non-attribute version of #1).

The ICloneable interface gives you a lot of questions, many of which the
answer to is "ICloneable is not enough" so I've decided that I won't use
it for my own objects except for trivial scenarios (ie. objects which
clearly aren't meant to participate in big hierarchies and graphs).

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2


Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dan | last post by:
without stirring the pot too much -- could someone please point me to whatever documentation exists on the philosophy, semantics, and practical implications of how Python implements the...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
11
by: Christoph Boget | last post by:
I've looked through the online documentation and I couldn't find a real explanation as to exacly what is involved with either of the above or exacly what they mean. If someone could point me to...
1
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
22
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.