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

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 47978
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
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
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
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
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
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
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
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
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
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.