473,666 Members | 1,991 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 48052
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******** ********@tk2msf tngp13.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_ahea d_remove_this.o ptonline.net> schrieb im
Newsbeitrag news:vR******** *************@n ews4.srv.hcvlny .cv.net...
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******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP09.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_ahea d_remove_this.o ptonline.net> schrieb im
Newsbeitrag news:vR******** *************@n ews4.srv.hcvlny .cv.net...
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******** ********@tk2msf tngp13.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_ahea d_remove_this.o ptonline.net> wrote in message
news:h%******** *************@n ews4.srv.hcvlny .cv.net...
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
3137
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 declares one of the columns as type Single.
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
4896
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. MyClass contains const int ERROR_NONE = 1 const int ERROR_NO_DRIVE = 2 const int ERROR_INSUFFICIENT_DISK_SPACE = 3
13
28253
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 command methods. This is exactly what the Microsoft Data Access Application Block assembly does but I coded my own simple, custom class. I have a method named "ExecuteAggregate" that takes in a sql string like
6
3324
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 the return also has the middle initial so my string look something like this "FirstName M" I don't need the M. Can nay one show me an example on how to parse the
3
12985
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
6
2595
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. The mask and the value are passed into a method.
1
1752
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 parameters can be passed to the Byte.ToString(String) routine, and what they mean. I can't seem to find anything in the usual places. Thanks in advance.
6
4334
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 computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
8445
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
8356
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
8781
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...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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
4198
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...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.