473,598 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ToString Debug vs Display strategies & preferences

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!
Aug 19 '08 #1
2 1835
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",nu ll) ("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",nu ll) giving "Michael
Phelps" (arbitraily chosen "standard format), ToString("f",nu ll) giving
"Michael", ToString("l",nu ll) giving "Phelps", ToString("fl",n ull) giving
"Michael Phelps", ToString("lf",n ull) giving "Phelps, Michael" etc. (I would
probably also provide a ToString(string fmt) which just calls
IFormattable.To String(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",nu ll);
}

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" <ef******@yahoo .comwrote in message
news:po******** *******@newsfe0 1.iad...
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!

Aug 20 '08 #2
wow - great response

My instincts were to do something like what you say but wrriting the format
provider seems intimidating, Guess I have no excuse for not working one
through now.

Thanks!
"Clive Dixon" <clived at digita dot comwrote in message
news:uw******** ******@TK2MSFTN GP02.phx.gbl...
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",nu ll) ("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",nu ll) giving
"Michael Phelps" (arbitraily chosen "standard format), ToString("f",nu ll)
giving "Michael", ToString("l",nu ll) giving "Phelps", ToString("fl",n ull)
giving "Michael Phelps", ToString("lf",n ull) giving "Phelps, Michael" etc.
(I would probably also provide a ToString(string fmt) which just calls
IFormattable.To String(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",nu ll);
}

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" <ef******@yahoo .comwrote in message
news:po******** *******@newsfe0 1.iad...
>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!


Aug 21 '08 #3

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

Similar topics

1
2076
by: Robert | last post by:
Simple way of writing debug print statements in your JavaScript code. You need to have enabled popups in your browser. I liked the ability to write to the java console in Netscape 4.x. This, ability is missing in IE and the latest versions of Netscape. Maybe someone knows a better way and can improve on this code, the following JavaScript code demonstrates how to write debug statements in a JavaScript program. I sure lots of people...
3
2403
by: clintonG | last post by:
I'm messing around with the Request.UserLanguages and the Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"); to see how similar or dissimilar they may be. I observe and conclude that both get their value from the Accept-Language key stored in the header of the response. To test this hypothesis I reconfigure IE Language Preferences which by default (for me) is English (United States). I include a Spanish (Mexico) Language Preference and
6
4665
by: Joe | last post by:
Stupid question, but I'm really stuck I have a class that overrides ToString(). When this class is cast back to Object, and ToString() called, Object.ToString() is called instead. I've tried everything: Public Overrides Function ToString() as String, Overrides Function ToString() as String, Overrides Overloads ToString() as string, Shadows ToString() as String,
3
1029
by: Nathan R. | last post by:
Hi, I'm seeing something that seems extremely strange to me. All I'm doing is overriding the toString method in one of my classes, so that instances will display the way I'd like in Combo boxes and the like. But instead of calling the overridden method of the subclass, it looks like Object.toString is being called instead -- toString returns "MyProjectName.MyClassName". So that's strange and mysterious, but that's not the end of it. ...
101
19168
by: Sean | last post by:
Book I am reading says that Cstr() is best method for efficency and safety however it doesnt compare that method of the .ToString() method. Which is best. Thanks
0
2363
by: bcobra | last post by:
What am I doing wrong? the code: age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %> <MM:DataSet id="SSF2Pinky"
10
10762
by: Immortalist | last post by:
Various aquisition devices that guide learning along particular pathways towards human biases. And as E.O. Wilson might say mental development appears to be genetically constrained. (1) Language Aquisition Device (2) Color Aqusition Device (3) Sound Aquistion Device (4) Smell Aquisition Device (5) Touch Aquisition Device (6) Art Aquisition Device
3
1166
by: gbgeek | last post by:
I've looked around the web a bit and didn't find much luck in locating a solution. Here the bit of code that I'm working with: strMyValue = ""; for (double d = double.MinValue; d < double.MaxValue && numPoints < maxPoints; d+=1.1F) { strMyValue = d.ToString(); //Here I hand off the string to be used by another object
2
3256
by: wreed06 | last post by:
Hello, I have 2 problems. In my webpage, I have a dropdown list with a button that takes the user to a popup window specific to the option. I am using Firefox 2.0.0.13. I have successfully validated my HTML and CSS code. 1. When I clear cache and refresh my webpage, it takes 3 tries before the popup window displays - I click on the button once, a white window the size of my webpage displays. I close it and click on the button again (for...
0
7902
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8395
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8050
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8265
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6719
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.