472,781 Members | 1,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,781 software developers and data experts.

Does Clone really make a new copy ?

Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

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

Wouldn't you?


Mar 7 '07 #1
18 2621
On Mar 6, 7:58 pm, " active" <activeNOS...@a-znet.comwrote:
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

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

Wouldn't you?
Actually, no. If you read the docs on string.equals:
true if the value of the value parameter is the same as this instance;
otherwise, false.

Equals on string has been overloaded to return value equality :)

--
Tom Shelton

Mar 7 '07 #2
It would be important to read the docs on String.Clone() as well. It
doesn't "clone" the string but simply returns another reference. So both
the equality test is True (which one would expect) and the "Is" test is True
(which one would expect wouldn't be the case.)

See: String.Copy() to create a separate object which will pass the equality
test but fail the Is test (demonstrating they aren't the same object.)

"Tom Shelton" <to*********@comcast.netwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Mar 6, 7:58 pm, " active" <activeNOS...@a-znet.comwrote:
>Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

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

Wouldn't you?

Actually, no. If you read the docs on string.equals:
true if the value of the value parameter is the same as this instance;
otherwise, false.

Equals on string has been overloaded to return value equality :)

--
Tom Shelton

Mar 7 '07 #3
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. There are 2 kinds of copies.
The shallow copy and the deep copy. The last means copy everything. For the
latter you can mostly use CopyTo, although than you change the object as
well. For a real deep copy you have mostly to serialize and to desterilize.

This is not always consistent in Net, by instance the classes from AdoNet
have another behaviour.

Clone means here copy of an empty object (the same as instancing a new
class, but can be handy if you have created everything yourself without a
strongly typed class)

Copy means here really copy of the total object.

Cor

" active" <ac**********@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

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

Wouldn't you?


Mar 7 '07 #4
On Mar 6, 9:21 pm, "Tom Leylan" <tley...@nospam.netwrote:
It would be important to read the docs on String.Clone() as well. It
doesn't "clone" the string but simply returns another reference. So both
the equality test is True (which one would expect) and the "Is" test is True
(which one would expect wouldn't be the case.)

See: String.Copy() to create a separate object which will pass the equality
test but fail the Is test (demonstrating they aren't the same object.)
Very true.

Dim s1 As String = "Hello, world!"
Dim s2 As String = DirectCast(s1.Clone(), String)
MessageBox.Show(String.Format("s1.Equals(s2) = {0}, s1 Is s2 =
{1}", _
s1.Equals(s2), s1 Is s2))

Dim s3 As String = String.Copy(s1)
MessageBox.Show(String.Format("s1.Equals(s3) = {0}, s1 Is s3 =
{1}", _
s1.Equals(s3), s1 Is s3))

In both cases, .Equals returns true - but, s1 is not s3 :)

--
Tom Shelton

Mar 7 '07 #5
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

" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

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

Wouldn't you?


Mar 7 '07 #6

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uI**************@TK2MSFTNGP02.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 7 '07 #7
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


Mar 7 '07 #8
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


Mar 7 '07 #9
active wrote:
Console.WriteLine(String.Equals(s, s.Clone.ToString))

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.

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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 7 '07 #10


"Göran Andersson" <gu***@guffa.comwrote in message
news:ug**************@TK2MSFTNGP03.phx.gbl...
active wrote:
>Console.WriteLine(String.Equals(s, s.Clone.ToString))

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.cloneMenu 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**************@TK2MSFTNGP06.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 "independent" 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**************@TK2MSFTNGP05.phx.gbl...
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uI**************@TK2MSFTNGP02.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.comwrote in message
news:%2****************@TK2MSFTNGP03.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
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...
6
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...
6
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...
16
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...
14
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...
1
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...
7
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. ...
5
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. ...
5
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.