473,406 Members | 2,745 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,406 software developers and data experts.

ToString - meaningful Vs. Useless

hi,

without going case-by-case, how do I know a "ToString" method returns useful
information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).

Ron
Aug 24 '06 #1
12 1179
Have you read the documentation to see what a given method call can return?
Its usually a good indicator as to whether a method will be useful or not in
the context in which you call it.
--
Regards

John Timney (MVP)
"Ron M. Newman" <co**********@confideltial.comwrote in message
news:eS**************@TK2MSFTNGP06.phx.gbl...
hi,

without going case-by-case, how do I know a "ToString" method returns
useful information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).

Ron

Aug 24 '06 #2
Let me clarify.

let's take the "Byte" structure for instance, and the "Bitmap" class.

Both have ToString. However, in Bitmap this method only returns the class
name, which is meaningless and useless because it doesn't override the
"ToString" from the Object class. The Byte structure however knows how to
repersent itself as string becusae it does override the "ToString" method.

My quetion: How do I tell which class that has a "ToString" is meaningful,
and which one isn't. this is essential to the type of serialization I'm
doing. do I have to put switch/case logic for every available object to make
the decision manually? or is there an all-inclusive rule I can use?

thanks
Ron

"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:k9********************@eclipse.net.uk...
Have you read the documentation to see what a given method call can
return? Its usually a good indicator as to whether a method will be useful
or not in the context in which you call it.
--
Regards

John Timney (MVP)
"Ron M. Newman" <co**********@confideltial.comwrote in message
news:eS**************@TK2MSFTNGP06.phx.gbl...
>hi,

without going case-by-case, how do I know a "ToString" method returns
useful information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).

Ron


Aug 24 '06 #3
"Ron M. Newman" wrote:
without going case-by-case, how do I know a "ToString" method returns useful
information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).
In 2.0, if it implements IConvertible, you should be able to rely on
the reversibility of the ToString method.

In 1.x, I suspect that having a Parse(string) method is a pretty
reliable indicator.

Beyond that, if it uses Object.ToString, then the result will just be
the type name. I'd imagine the odds are good that any type that
overrides ToString is returning a formatted version of the content,
especially for value types.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 24 '06 #4
Hi,

Thanks. Eventually I did just that, I'm checking for a local ToString
implementation that's indigenous to the type and not derived from
elsewhere - this is a good indication for me that the output would be
meaningful.

Thanks for understanding what I was looking for :-)

Ron

"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:44**************@midnightbeach.com...
"Ron M. Newman" wrote:
>without going case-by-case, how do I know a "ToString" method returns
useful
information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).

In 2.0, if it implements IConvertible, you should be able to rely on
the reversibility of the ToString method.

In 1.x, I suspect that having a Parse(string) method is a pretty
reliable indicator.

Beyond that, if it uses Object.ToString, then the result will just be
the type name. I'd imagine the odds are good that any type that
overrides ToString is returning a formatted version of the content,
especially for value types.

--

.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.

Aug 25 '06 #5
Your question is not clear. What do you mean by "meaningless?" When the
ToString method is overridden in a class, the developer that overrides it
decides what the implementation should produce. I'm sure that this is for
the purpose of creating something "meaningful" out of the method, but what
the "meaning" is depends upon the class and the implementation of the
override. In other words, things are meaningful to different people at
different times for different reasons.

In fact, most overrides of the ToString method are meaningful only to
developers. They provide quick information about a class during the
debugging process. However, other overrides are useful for other purposes,
such as the override of ToString on Enums that displays the token of the
enumerated value.

I do not know of any way to tell whether or not a method is overridden in an
inherited class. It is probably possible to do this by using Reflection,
perhaps by getting a MethodInfo from both the base and derived class and
comparing them. But still, you aren't going to know whether the ToString
method implementation is "meaningful" to you. In fact, you haven't even
*defined* "meaningful."

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Ron M. Newman" <co**********@confideltial.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Let me clarify.

let's take the "Byte" structure for instance, and the "Bitmap" class.

Both have ToString. However, in Bitmap this method only returns the class
name, which is meaningless and useless because it doesn't override the
"ToString" from the Object class. The Byte structure however knows how to
repersent itself as string becusae it does override the "ToString" method.

My quetion: How do I tell which class that has a "ToString" is meaningful,
and which one isn't. this is essential to the type of serialization I'm
doing. do I have to put switch/case logic for every available object to
make the decision manually? or is there an all-inclusive rule I can use?

thanks
Ron

"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:k9********************@eclipse.net.uk...
>Have you read the documentation to see what a given method call can
return? Its usually a good indicator as to whether a method will be
useful or not in the context in which you call it.
--
Regards

John Timney (MVP)
"Ron M. Newman" <co**********@confideltial.comwrote in message
news:eS**************@TK2MSFTNGP06.phx.gbl...
>>hi,

without going case-by-case, how do I know a "ToString" method returns
useful information, like for Sytem.Int32, or useless information like
for System.Drawing.Bitmap (returning the name of the class).

Ron



Aug 25 '06 #6
Kevin Spencer wrote:
I do not know of any way to tell whether or not a method is overridden in an
inherited class. It is probably possible to do this by using Reflection,
perhaps by getting a MethodInfo from both the base and derived class and
comparing them.
Just get the MethodInfo and look at the DeclaringType property.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 25 '06 #7
Thanks Jon. I'll file that tidbit away!

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:44***************@midnightbeach.com...
Kevin Spencer wrote:
>I do not know of any way to tell whether or not a method is overridden in
an
inherited class. It is probably possible to do this by using Reflection,
perhaps by getting a MethodInfo from both the base and derived class and
comparing them.

Just get the MethodInfo and look at the DeclaringType property.

--

.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.

Aug 25 '06 #8
I'll join you in filing that, cos thats a useful snippet to know!!

Regards

John Timney (MVP)
"Kevin Spencer" <uc*@ftc.govwrote in message
news:ep*************@TK2MSFTNGP02.phx.gbl...
Thanks Jon. I'll file that tidbit away!

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:44***************@midnightbeach.com...
>Kevin Spencer wrote:
>>I do not know of any way to tell whether or not a method is overridden
in an
inherited class. It is probably possible to do this by using Reflection,
perhaps by getting a MethodInfo from both the base and derived class and
comparing them.

Just get the MethodInfo and look at the DeclaringType property.

--

.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.


Aug 25 '06 #9

Kevin Spencer wrote:
In fact, most overrides of the ToString method are meaningful only to
developers. They provide quick information about a class during the
debugging process. However, other overrides are useful for other purposes,
such as the override of ToString on Enums that displays the token of the
enumerated value.
I believe I read somewhere that the framework designers only intended
the ToString method to be used for debugging. So what you said makes
sense.

Brian

Aug 26 '06 #10
Brian Gideon wrote:
I believe I read somewhere that the framework designers only intended
the ToString method to be used for debugging.
You might have read it, but it can't be true - look at the way that
String.Format uses ToString, as do the String.Concat overloads that
take object parameters.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 26 '06 #11

Jon Shemitz wrote:
Brian Gideon wrote:
I believe I read somewhere that the framework designers only intended
the ToString method to be used for debugging.

You might have read it, but it can't be true - look at the way that
String.Format uses ToString, as do the String.Concat overloads that
take object parameters.

--

.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
It's certainly possible that I just made that up. In fact, come to
think of it, what I might have read said something about always
overriding ToString so that, if nothing else, it could be used for
debugging. It's just as likely I made that up too.

Brian

Aug 27 '06 #12
Ron,

I thought I have not seen it written.

ToString is a member from Object. In Net everything is inheriting from
object and therefore every object has a to string method. If it is useful
depends if it is overridden or shadowed by either you or by the base class
that you are using.

http://msdn2.microsoft.com/en-us/lib...t_members.aspx

Something the same as the Dispose, which is a member of the component class
which is the base class of about 20% of (probably the most used) in Net.

http://msdn.microsoft.com/library/de...mberstopic.asp

You can see that in this class the ToString is overridden and therefore has
much more meaning.

I hope this helps,

Cor

"Ron M. Newman" <co**********@confideltial.comschreef in bericht
news:eS**************@TK2MSFTNGP06.phx.gbl...
hi,

without going case-by-case, how do I know a "ToString" method returns
useful information, like for Sytem.Int32, or useless information like for
System.Drawing.Bitmap (returning the name of the class).

Ron

Aug 27 '06 #13

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

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
3
by: windandwaves | last post by:
Hi, I am trying to make errors a bit more meaningful for my users. I am trying to fix error 3201 where there is a missing field in a form. If I do not do any error trapping, then I get ...
3
by: Fredrik Wahlgren | last post by:
Hi I found an interesting piece of code which shows how to makee an Excel automation add-in at http://www.codeproject.com/dotnet/excelnetauto.asp. I decide to experiment with it and i made a few...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
11
by: Rickie | last post by:
Dear fellow software engineers, Is the web menu in asp 2.0 essentielle a useless control ? How many people( including msdn itself) are using server menu controls? Its a menu which always...
2
by: Srini | last post by:
Hi, Can anyone please tell me how to retrieve meaningful value from registry value in REG_Binary type? I tried to convert REG_Binary value to string using the following code.
9
by: Lars Eighner | last post by:
In the code below, the line "unsigned int ktop,kbot;" seems to be useless as ktop and kbot are never used. Yet, this work with it and breaks without it. Any idea why? #include <stdio.h>
38
by: tshad | last post by:
In VS 2008, why doesn't ToString require "()". If I have Option Strict On on why can I do both: selectedIndex.ToString() selectedIndex.ToString or sQuery =...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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...
0
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,...
0
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...

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.