Why not use an enum for this purpose ? Personally, I try to
avoid
string constants like the plague because they are not easily
discoverable. Or at least a constants class.
What they could have done with DateTime is provide public
constants
for the various formats. eg.
static class DateTimeFormats
{
public const string ShortYear="yy";
public const string LongYear="yyyy";
//...
}
in your code:
string s = DateTime.Now.ToString(DateTimeFormats.LongDate);
Sébastien
On 8/2/06, Robert Rolls <implatform netspace.net.au> wrote:
> 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: Discussion of development on the .NET platform
using any managed
> language [mailto OTNET-CL
R DISCUSS.DEVELOP.COM] On Behalf Of Ian Griffiths
> Sent: Tuesday, August 01, 2006 2:26 AM
> To: DOTNET-CLR DISCUSS.DEVELOP.COM
> Subject: Re: [DOTNET-CLR] object.ToString() or
TypeConverter
>
> 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 <richard DOTNETCONSULT.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. http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
--
Sébastien Lorion
Software Architect / Architecte organique
sebastien.lorion nurun.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|