display boolean as Yes/No

  • Thread starter Thread starter Eric Gurney
  • Start date Start date
E

Eric Gurney

Is there a way to change the output of the ToString function for a boolean
variable to display Yes/No?

Thanks,
Eric
 
From: "Eric Gurney" <[email protected]>

| Is there a way to change the output of the ToString function for a boolean
| variable to display Yes/No?

To change the behavior of a type, you have to change its source code.
Since bool is a built-in type, you cannot change its behavior.

The easiest thing to do is to have something like:

public static string BoolToYesNo( bool b ) { return b ? "Yes" : "No"; }

You can put this method inside one of your utility classes.

Another way is to define your own boolean type whose ToString() method
return "Yes" or "No". If you go down this path, you can choose to
implement the IFormattable interface and have more formatting options.

- Zhanyong Wan

Visual Studio and .NET Setup

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included samples (if any) is subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
That's pretty much what I was going to do. I was looking for a more elegant
solution but I guess this works.
Why didn't MSFT allow the IFormatProvider interface to be used with the bool
type?
 
Back
Top