473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ToString() and (string) cast

If you override ToString(), why can't the default implementation of the
string cast use that implementation for the object? It's a question people
ask me time and time again.

Nov 15 '05 #1
6 48018
Hi Jhon,

ToString() returns only a value, which is representing the object like
int.ToString() returns the value as a string or somtemis it returns the type
or name or somethin else.

with the cast (string) you cast the object into a string object.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de
Nov 15 '05 #2
Right, but seeing as there's little difference between (string)3 and
3.ToString() -- I don't see why the compiler can't choose to use ToString()
when no string cast is available in the former example.

"Ralph Gerbig" <ik********@web.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Jhon,

ToString() returns only a value, which is representing the object like
int.ToString() returns the value as a string or somtemis it returns the type or name or somethin else.

with the cast (string) you cast the object into a string object.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de

Nov 15 '05 #3
string msg;
int i = 5;
object o;

msg = i.ToString(); // Kopies the value i has into msg
msg = (string)i; // Trys to cast i into a string object and copy a pointer
into msg (this statement won't work)
o = msg; // copys a pointer on msg into o (no explicit cast is needed
because string is inherited from System.Object)

there is a huge diference in copying a pointer and changing a value.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de
"John Wood" <jwood8@go_ahead_remove_this.optonline.net> schrieb im
Newsbeitrag news:vR*********************@news4.srv.hcvlny.cv.n et...
Right, but seeing as there's little difference between (string)3 and
3.ToString() -- I don't see why the compiler can't choose to use ToString() when no string cast is available in the former example.

"Ralph Gerbig" <ik********@web.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Jhon,

ToString() returns only a value, which is representing the object like
int.ToString() returns the value as a string or somtemis it returns the

type
or name or somethin else.

with the cast (string) you cast the object into a string object.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de


Nov 15 '05 #4
I see what you're saying. There's no hierarhical relationship between an int
and a string, and that's what casting is for. So I suppose ToString() is
really just a conversion routine, not a casting routine.

Thanks for clarifying.

"Ralph Gerbig" <ik********@web.de> wrote in message
news:ur**************@TK2MSFTNGP09.phx.gbl...
string msg;
int i = 5;
object o;

msg = i.ToString(); // Kopies the value i has into msg
msg = (string)i; // Trys to cast i into a string object and copy a pointer
into msg (this statement won't work)
o = msg; // copys a pointer on msg into o (no explicit cast is needed
because string is inherited from System.Object)

there is a huge diference in copying a pointer and changing a value.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de
"John Wood" <jwood8@go_ahead_remove_this.optonline.net> schrieb im
Newsbeitrag news:vR*********************@news4.srv.hcvlny.cv.n et...
Right, but seeing as there's little difference between (string)3 and
3.ToString() -- I don't see why the compiler can't choose to use

ToString()
when no string cast is available in the former example.

"Ralph Gerbig" <ik********@web.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Jhon,

ToString() returns only a value, which is representing the object like
int.ToString() returns the value as a string or somtemis it returns
the type
or name or somethin else.

with the cast (string) you cast the object into a string object.

--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
ik********@web.de



Nov 15 '05 #5
On Sun, 10 Aug 2003 19:16:56 +0200, "Ralph Gerbig" <ik********@web.de>
wrote:
string msg;
int i = 5;
object o;

msg = i.ToString(); // Kopies the value i has into msg
msg = (string)i; // Trys to cast i into a string object and copy a pointer
into msg (this statement won't work)


The difference that your comments describe does not exist. Both
ToString() and (string) generate a System.String that is (hopefully)
somehow based on the value of i. The second statement would work just
fine if i happened to be of a type that defines a typecast to string.

John Wood is correct, there really is no difference between the two
operations, and the compiler might easily map the string cast to the
ToString method by default.
--
http://www.kynosarges.de
Nov 15 '05 #6
I think one consideration against such a default mapping is that some
objects might not really *want* to be able to be cast to a string. If there
isn't a possible string representation of the object, it makes sense that
the object wouldn't like to be cast to a string. ToString exists mainly, I
think, so that *any* object can be represented at least *minimally* as a
string. But the presence of a string cast implies as a matter of design that
the casted object is somehow a complete or adequate representation of that
object, which is not always possible, and less often the case.

Chris

"John Wood" <jwood8@go_ahead_remove_this.optonline.net> wrote in message
news:h%*********************@news4.srv.hcvlny.cv.n et...
If you override ToString(), why can't the default implementation of the
string cast use that implementation for the object? It's a question people
ask me time and time again.

Nov 15 '05 #7

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

Similar topics

14
3116
by: Neil Guyette | last post by:
Hello, I'm having problems figuring out why a conversion between a System.Single type roundd when calling the object's ToString method. I'm pulling data out of an OdbcDataReader which...
6
452
by: roni | last post by:
i have array of objects that's are strings. i need to convert it to array of string before sending to method. is there a way to convert ? (short way..)
5
4881
by: Daniel Bass | last post by:
I've got some class which contains loads of static const int values, is there a way that, given an int, i can quickly cast the int back to a string representation of the const? for eg. ...
13
28232
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
6
3316
by: Netmonster | last post by:
Hello all, I am creating a string based on data returned from an oracle db into a hashtable. string tmpFirstName = tmphashtable.ToString(); The Database has a column called FIRST_NAME but...
3
12947
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
6
2582
by: jmanion | last post by:
Hello, I have a problem, I'm hoping someone can help. I have a string, lets say: "111111"; // Not always numeric I have a format mask, lets say: "(00) 0000"; // Again, not always numeric. ...
1
1738
by: Carona | last post by:
Hi, I've been converting a byte to a hex string using "X2" as the format. However, I've taken this directly from the web and being of the curious type I was wondering what other possible format...
6
4330
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
7114
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...
0
7377
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...
1
7034
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...
0
5623
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,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.