473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does Clone really make a new copy ?

Console.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?


Mar 7 '07
18 2660


"Göran Andersson" <gu***@guffa.co mwrote in message
news:ug******** ******@TK2MSFTN GP03.phx.gbl...
active wrote:
>Console.WriteL ine(String.Equa ls(s, s.Clone.ToStrin g))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?

The purpose of the Clone method is to create a separate copy of the
object.
Actually I've used MenuItem.cloneM enu when I needed a second copy and that
always seemed to work.

>
The String class makes an exception to this and returns a copy of the
reference to itself. As the String class is immutable, it doesn't make any
difference if you get a copy of the reference or a recerence to a copy.
I suppose one might want a copy of a string (maybe delivered by an event for
example) with new protections or scope.

If I want a new reference I would do it with the = sign

Why use clone for that.

So why make an exception - Consistency is very important

I used String.Copy to make the clone
Thanks a lot for straighten it out for me
>
--
Göran Andersson
_____
http://www.guffa.com

Mar 7 '07 #11
active wrote:
>The String class makes an exception to this and returns a copy of the
reference to itself. As the String class is immutable, it doesn't make any
difference if you get a copy of the reference or a recerence to a copy.

I suppose one might want a copy of a string (maybe delivered by an event for
example) with new protections or scope.
The string data doesn't have any scope at all. Only the references to
the data have scope.

The only reason to ever create a copy of a string would be if you want
two strings with the same value but with different references. I however
see no practical use for that at all. What's interresting about a string
is it's value, where the actual data is stored is irrelevant.
If I want a new reference I would do it with the = sign

Why use clone for that.
The Clone method is just implemented that way because there is no reason
to a lot more work than is needed.
So why make an exception - Consistency is very important
The implementation of String.Clone follows the intention of the
interface. It returns a string that won't change if the original string
is assigned a new value.

As string data never change, any number of references can safely point
to the same string data without ever risking that the data is changed by
code elsewhere.
I used String.Copy to make the clone
Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com
Mar 7 '07 #12
Active,
>
But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
I wished it was the only one, however once it is started in use it is
unchangable, what brings us direct on one of the most awtfull ones as
example. Acceptchanges. This does not accept the changes. It changes the
rowstate in a datarow and set that to done.

However the term shallow does describe it maybe nice

http://www.thefreedictionary.com/shallow
Cor
Mar 7 '07 #13
OK

thanks

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:Os******** ******@TK2MSFTN GP06.phx.gbl...
Active,
>>
But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
I wished it was the only one, however once it is started in use it is
unchangable, what brings us direct on one of the most awtfull ones as
example. Acceptchanges. This does not accept the changes. It changes the
rowstate in a datarow and set that to done.

However the term shallow does describe it maybe nice

http://www.thefreedictionary.com/shallow
Cor

Mar 7 '07 #14
All I know is I have an event that returns a string which I use in creating
a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item displays
OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the item
has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now academic.
Thanks for the help

>
Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com

Mar 7 '07 #15
A shallow copy does not mean "copy only the reference". The definition of
the term (in DotNet at least) is that a copy of the "contents" of the
properties is created. In the case of a reference type the contents is a
reference and as such you end up with two objects pointing to some shared
data. Value types would not exhibit this behavior so you in fact have some
sort of hybrid object.

As to why it is called "Clone" that is because the String class implements
ICloneable. The actual implementation is dependent upon what a class
typically does and Goran Andersson has pointed out very nicely why there is
little need to create another "independen t" string in order to create a
clone of a string. By their nature strings are immutable and a clone must
be identical so one copy of the value will suffice. Truth be told string
constants are compiled into the app and anything which needs the sequence
"Process Complete" (for instance) is pointing at the one copy of that string
regardless of how many times it appears in the code.

Tom
" active" <ac********** @a-znet.comwrote in message
news:OJ******** ******@TK2MSFTN GP05.phx.gbl...
>
"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:uI******** ******@TK2MSFTN GP02.phx.gbl...
>active,

In most cases the Clone creates a "shallow" copy. This means that the
clone, copies only the reference.

In fact does mostly "copy" methods the same.
Could you say the above a little differently - I'm not sure what you mean.


Mar 8 '07 #16
active wrote:
Thanks for the help.

But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
Thanks
The purpose of the Clone method is to create a copy that is safe from
changes in the original.

As strings are immutable, the string class can make the exception to
just copy the reference. Eventhough the references point to the same
string data, they can safely do so because the string data never changes.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #17
Althought it seems to work at the moment, I think that you should try to
investigate the matter further.

Copying a string is normally never required. The reason that copying the
string makes it work might be because it's causing some side effect that
is affecting something else in the system. The risk is that whatever is
wrong might reappear at any time, or appear somewhere else in the
application.

active wrote:
All I know is I have an event that returns a string which I use in creating
a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item displays
OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the item
has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now academic.
Thanks for the help

>Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com
--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #18
I spent over a day looking, lots of single stepping and trying anything I
could think of.

I don't know what else to try.

Thanks for the interest
"Göran Andersson" <gu***@guffa.co mwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Althought it seems to work at the moment, I think that you should try to
investigate the matter further.

Copying a string is normally never required. The reason that copying the
string makes it work might be because it's causing some side effect that
is affecting something else in the system. The risk is that whatever is
wrong might reappear at any time, or appear somewhere else in the
application.

active wrote:
>All I know is I have an event that returns a string which I use in
creating a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item
displays OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the
item has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now
academic.
Thanks for the help

>>Why do you want a separate copy of a string anyway? It only wastes
memory.
--
Göran Andersson
_____
http://www.guffa.com

--
Göran Andersson
_____
http://www.guffa.com

Mar 8 '07 #19

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

Similar topics

0
2664
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some not so obvious errors I have made.. Cheers
6
12904
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an object, what do I assign it to in my derived class? public class Base : ICloneable { .... public object Clone() {
6
2148
by: Steve | last post by:
I can't find a straight answer on what to use? I need a deep copy, so I implemented IConeable and the Clone() method. However, I'm not sure I did it correct. Is it suposed to be an allocation of a new object, then the assignment of each member? Is that all or is there something else I need to do? Here is my code <code> public Object Clone() {
16
2509
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some objects (like Font, Style, String,
14
8617
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of the old control in runtime and set my new cloned control events to the event handlers of the old...
1
2505
by: woessner | last post by:
I have a lot of classes which derive from a common base class. I want to be able to clone objects of these types so I have given the base class a pure virtual clone method. The derived classes all have complete copy constructors so the cloning process will consiste of "return new Derived(*this);". Here's the setup: struct Base { virtual Base* Clone() const = 0; };
7
2333
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
5
1866
by: DavidB | last post by:
I have a situation where a user needs to clone an existing BE database (stored on a server) to his local machine. Assume the followings... User goes into the database FE which has linked BE. BE is stroed on a server. User needs to be able to click a button on the FE that will clone the copy of the BE on the server to C:\. If a copy of the BE already exists at C:\, the clone process should overwrite the existing copy at C:\.
5
2096
by: raylopez99 | last post by:
In C++, you have symbolic overriding of "+", "=", etc, which C# also has. This question is not really about that. Rather, in C#, when you say: MyObject X = new MyObject(); MyObject Y = new MyObject(); X = Y; //what does this '=' mean?
0
8685
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
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
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
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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.