List Info

Thread: object.ToString() or TypeConverter




object.ToString() or TypeConverter
user name
2006-08-03 22:26:25
Mr I,

Agreed it's still the same value - apart from the tooltip
wickidie whack
idea. Code/Decode still seems like a reasonable use of
IFormattable. 

Thanks, for the comments.

Rob.

-----Original Message-----
From: Discussion of development on the .NET platform using
any managed
language [mailtoOTNET-CL
RDISCUSS.DEVELOP.COM] On Behalf Of Ian
Griffiths
Sent: Thursday, 3 August 2006 11:58 PM
To: DOTNET-CLRDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-CLR] object.ToString() or TypeConverter

But it's all still the same value.

Culturally we have several different ways of representing
dates. But
it's fairly clear in certain popular locales that
12/12/2006,
2006/12/12, 2006-12-12, 12th December 2006, December 12th
2006,
12-Dec-2006, and Dec-12-2006 are all the same value.

And that's the point of IFormattable - it's a way of
choosing the
representation.

That seems to me like a completely different kind of thing
from
selecting the value to be represented.

But this example does illustrate an interesting gray area.
You can
format a date in such a way that you only show some part of
the date.
Indeed, a DateTime includes a time portion, but none of the
representations above include that. I call this a gray area
because you
could regard that as being a matter of precision: just as I
can choose
with a number how many digits of precision I'd like to use
when
displaying it, so with a date I can choose the degree of
precision: just
the date, or the time too?

Where it gets interesting is if you're using a DateTime
just to hold a
time, and elect to display just the time and not the date.
That's
something that doesn't have a direct analogue with numeric
precision.

Even in this case, I think it's still fair to say that
ToString is still
only being used to display the value. It's just that there
is a common
use case for DateTime where the value is just a time. (And
in fact,
since the date aspects are usually zero in this case, you
could say it's
equivalent to removing leading zeros when formatting a
number. So maybe
there is a numeric analogue after all.)

So that does suggest that if you have a data type with
multiple
properties that constitute its whole value, and there are
use cases in
which you might not care about some aspects of the value,
then in that
case it might be appropriate to have formatting options that
omit parts
of the value.

But even having revised my opinion, I still think that using
this to
pick out a tooltip is the wrong thing to do. The tooltip
message isn't a
part of an object's value. It's a distinct value in its
own right with
its own unique purpose.


-- 
Ian Griffiths

-----Original Message-----
From: Robert Rolls
Sent: 02 August 2006 22:08

Ian,

Interestingly that calling ToString() on a DateTime of
12/12/2006 can
yield different representations i.e. it can display 12,
December 2006 I
see this as a kind of code/decode the only thing being
people expect
December as it's part of everyday life. Values within
applications also
being code/decode e.g., 145 can be 'Business User' 125
'Home User' would
this be fair use of
ToString("L") L=LongDescription?

Rob.

-----Original Message-----
From: Ian Griffiths
Sent: Tuesday, August 01, 2006 2:26 AM

True, but I think that using this to select between a number
of strings
that serves a completely different purpose would be a case
of stretching
things a little.

When you use IFormattable on any of the built-in types,
you're not
actually changing the data that is shown. You're just
controlling how
that data is shown. For numbers you can select things like
the
precision, the style and so on, but it's always the same
number.

It's a clever hack to use IFormattable as a solution to
this problem,
but I think it's ultimately rather confusing. It's very
non-discoverable, and also really rather different from how
IFormattable
is normally used. Moreover, it's at odds with what the
documentation for
IFormattable.ToString says. (Or at least, I don't think
that "the value
of" is synonymous with "the value of one of the
properties of"...)

So on the 'principle of least astonishment' basis, I'd
avoid using
IFormattable as a means of selecting between different data
items to be
returned from ToString.

If an object has a number of different pieces of information
associated
with it, then the most obvious way to expose those is
through a number
of properties. After all, I think the following is pretty
readable:

  string msg = myObj.ToolTipMessage;

whereas this is a lot less clear:

  string msg = myObj.ToString("T");

or

  string msg = string.Format("{0:T}", myObj);



--
Ian Griffiths
http://www.int
eract-sw.co.uk/iangblog/

> -----Original Message-----
> From: Barry Kelly
>
> Richard Blewett <richardDOTNETCONSULT.CO.UK>
wrote:
>
> > Ah ok - I see what you're getting at.
> >
> > But surely the ToolTip would be served (ignoring
the built in
support)
> by a
> > ToolTip property
>
> Let's not forget that there's built-in support for
exactly specifying 
> the kind of ToString() via a passed format
specification
(IFormattable),
> which can also be used by string.Format etc.

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )