473,847 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3754
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******** ********@TK2MSF TNGP09.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******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP09.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******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP09.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 "indepepend ent 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******** ********@TK2MSF TNGP14.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******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP09.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2338
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten it. Perhaps there are some python features or libraries I'm not taking advantage of. The way it works is that it puts references to all the files in a dictionary with file size being the key. The dictionary can hold multiple values per key....
1
3000
by: JStrummer | last post by:
I just put together a PHP mailing list sign-up page that posts to a mySQL DB. The PHP script prevents INSERTs when the email address is already located in the database. Problem: I need to import some flat-files that stored the signups, prior to this new form. Email addresses weren't checked, so there are a lot of records that have the same email address. Once I import these into the table, how would I go about putting together a SQL...
4
2971
by: GlobalBruce | last post by:
The global assembly cache on my development computer has duplicate native images of several System assemblies. For instance, the System assembly itself is present as two different native images, as well as a non-native version. The native images have different date/time stamps -- although they differ only by 3 days in Feb/2004. Many of the System.* packages have similar redundant native images. Why are there redundant native image...
3
28849
by: Nhmiller | last post by:
I searched here for an answer. I am entering records into a database, and it would save a lot of time if I could duplicate a record that is very similar to the new one I am about to enter, then make one or two changes to the new one. This is the problem I have with Access. Look at this answer! >Private Sub btnCopy_Click() >On Error GoTo handle_error > Set cnxn = CurrentProject.Connection > Set rs = New ADODB.Recordset
0
1131
by: Nbene | last post by:
Hi All, Hoping someone can help with this one. I am opening a table and looping through the RecordSource (RcSr) of ! Email I am trying to add each email address to the string (strEmail) for use in sending to outlook.
5
3072
by: Shawn Northrop | last post by:
_root.text_mc.duplicateMovieClip("test",1); _root.test._x="10"; _root.test._y="10"; I have a mc called text_mc Inside is a colored line. There is no instance of this clip, it is in the library. I am trying to generate an instance. The above code seems to not do anything. Is there something i am missing? Eventually i would like to put this in a loop and have: duplicate....("test"+i,i); _root.test+i._x=..... ??????
6
4112
by: Arthur Dent | last post by:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the same event handler on an event? e.g... I have some object which raises an event, and some other class which consumes them. So in essence what I want to do is something like this in the consumer class... If Not objEventSource.SomeEvent.Handlers.Contains(AddressOf me.EventHandler) Then _ AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
1
4421
by: traceable1 | last post by:
I have a table with a unique non-clustered index on it. It has the IGNORE_DUP_KEY option on. For some reason, I am getting the following error: ----------------------------------------------------------------------------- System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.table1 with unique index 'IX_table1'. Duplicate key was ignored.
7
4673
by: swami | last post by:
What is the query for selecting non duplicate elements for eg: no name age 1 siva 28 2 blair 32 3 mano 28 i want to select blair which hasn't got any duplicate elements in age
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9734
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5725
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4129
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.