ToString - meaningful Vs. Useless

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

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
 
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.
 
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
 
Ron M. Newman said:
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.
 
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
 
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.
 
Kevin said:
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.
 
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.
 
I'll join you in filing that, cos thats a useful snippet to know!!

Regards

John Timney (MVP)


Kevin Spencer said:
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.
 
Kevin said:
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
 
Brian said:
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.
 
Jon said:
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.

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
 
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/library/system.object_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/d...systemcomponentmodelcomponentmemberstopic.asp

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

I hope this helps,

Cor
 
Back
Top