Re: ToString Debug vs Display strategies & preferences



For the kind of scenario you give as an example, I would give the class an
IFormattable implementation as well as ToString() override. Then a user of
the class can format the name in any of the ways the implementation
supports, rather than being confined to a fixed way of doing it. Your
ToString() override would then just call the ToString("g",null) ("g" for
generic/general, following the framework implementation for standard format
strings) of the IFormattable. You could have format strings "f" and "l" for
first name and last name, and so call ToString("g",null) giving "Michael
Phelps" (arbitraily chosen "standard format), ToString("f",null) giving
"Michael", ToString("l",null) giving "Phelps", ToString("fl",null) giving
"Michael Phelps", ToString("lf",null) giving "Phelps, Michael" etc. (I would
probably also provide a ToString(string fmt) which just calls
IFormattable.ToString(fmt,null) to avoid having to pass a null all the
time.)

For debugging, I often use the DebuggerDisplay (or DebuggerProxy) attribute
on the class. Sometimes I just call ToString() from this, more often I will
individually access certain properties so it's clear what each property
value in the object is. It all depends on what I want to see for that
particular class.

// Alternative 1a
[DebuggerDisplay("Name = {ToString()}")]
// Alternative 1b
[DebuggerDisplay("Name = {ToString("lf")}")]
// Alternative 2
[DebuggerDisplay("FirstName = {FirstName}, LastName = {LastName}")]
class Person : IFormattable
{
public string FirstName { get; }
public string LastName { get; }

public override string ToString()
{
return ToString("g",null);
}

public string ToString(string fmt, IFormatProvider fp)
{
switch (fmt)
{
case "g":
default:
return string.Format("{0} {1}",FirstName,LastName);
case "f":
case "l":
case "fl":
case "lf":
//etc.
}
}
}

"Berryl Hesh" <efinger9@xxxxxxxxx> wrote in message
news:poIqk.257$RV7.105@xxxxxxxxxxxxxxx
I'm interested in how experienced.Net developers might handle routine
display tasks as a general strategy. Let's say you have a use in your
domain for value objects that encapsulate a person's Name, for example.

Would anyone write a custom format provider to display the last name first
("Phelps, Michael"), or first name first ("Michael Phelps")? Or use the
familiar name instead ("Mike"). Would you use some other technique? Would
you provide a Parse method?

How about generalized debugging ToString stratagies? Any favorite
techniques and / or tools? For example, "[FIRSTNAME = Mike, LASTNAME =
Phelps]".

TIA, and thanks for sharing your experience!



.



Relevant Pages

  • Re: ToString Debug vs Display strategies & preferences
    ... My instincts were to do something like what you say but wrriting the format ... IFormattable implementation as well as ToString() override. ... For debugging, I often use the DebuggerDisplay ... public string FirstName ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Databinding to a combo box: Newbie
    ... Override the ToString function to return the 'look' that you want the ... The combobox will display the ToString output but the Selected item ... public struct stTuple ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using ToString to pad an integer with leading zeroes
    ... ToStringto work when debugging. ... No overload for method 'ToString' takes '1' arguments ... What are you calling ToString() on? ... If you're writing code in the debugger's immediate window, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Data::Dumper for java
    ... one that is tunable (ie: that can override objects' ... toString() methods, shows only so many levels of output, etc. ... Java so helpfully provides a somewhat meaningless toStringfunction ... For debugging, my debugger shows me all I need, with out any work whatsoever on my part. ...
    (comp.lang.java.programmer)
  • Re: Data::Dumper for java
    ... Avoid having to write toString() functions for all of your modules, ... For debugging, my debugger shows me all I need, with out any work ... if you know how to run code on a server, serialize input and output ... via an ssh shell, and get the output into an intellij/eclipse IDE, I'd ...
    (comp.lang.java.programmer)