Boolean format provider


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.7K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I just had the need to provide some custom formatting for a boolean value other than the default. The default is to convert a true value to the string defined by the shared (static in C#) readonly property TrueString ("True") of the Boolean structure and a false value to the string defined by the shared (static in C#) readonly property FalseString ("False") of the Boolean structure.



If you need to display something else for a boolean, there is no default way within .NET to do this. They provide no nifty format codes that can be used with the String.Format() method (see note below) and while the ToString() method has an overload that accepts an IFormatProvider, MSDN states (and my tests verify) that the provider isn't ever used, even if provided.



Examples of what I'm trying to accomplish:



- field that indicates if a CD is loaded into a drive or not, display "Loaded" for true and "Unloaded" for false.

- field that indicates if a person is married, display "yes" for true, "no" for false.

- field that indicates if an employee is a manager, display "M" for true, empty string for false.



What I've done is create a class that implements IFormatProvider and ICustomFormatter. This allows me to then use the class with the String.Format() method to customize the display of boolean values. Here is how I'd implement the examples above:



Dim isCDLoaded As Boolean

MessageBox.Show(String.Format(New BooleanFormatProvider("Loaded|Unloaded"), isCDLoaded)

' shows 'Unloaded'

Dim isMarried As Boolean = True

MessageBox.Show(String.Format(New BooleanFormatProvider("yes|no"), isCDLoaded)

' shows 'yes'

Dim isManager As Boolean = True

MessageBox.Show(String.Format(New BooleanFormatProvider("M|"), isCDLoaded)

' shows 'M'

isManager = False

MessageBox.Show(String.Format(New BooleanFormatProvider("M|"), isCDLoaded)

' shows ''





This formatter lets you define the strings used for true and false values. You can also define the delimiter that is used between these parts. The constructors allow you define the default format string. See the sample for more details on how this is accomplished.



Please note this is my first attempt as coding either of these interfaces, so I'd appreciate any helpful hints/suggestions!







Note: E.g. if you have a Date variable myDate, you can get string formatted as a short date with String.Format by simply using:



String.Format("{0:d}",myDate)

Attachments
BooleanFormatProvider.zip (364 views, 682.00 KB)
Trent Taylor
Trent Taylor
StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Cool...good job, Greg!
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search