473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

iformatprovider for boolean

Hello

There doesn't appear to be an iformatprovider for to specify the format to convert a boolean to.

i.e. MyBool.ToString("YES/NO") does not work

Any workarounds?
Nov 20 '05 #1
1 5761
Eric,
Create a class that implements IFormatProvider, such that it returns an
object that implements ICustomFormatter. Within the ICustomFormatter.Format
method return the string that you desire...

Something like:

Namespace Gofoth

Public Class Eric

Public Shared Sub Main()

Dim s As String

' the format string is "true value; false value"

s = String.Format(New BooleanFormatter, "{0:Yes;No}", False)
s = String.Format(New BooleanFormatter, "{0:Yes;No}", True)
s = String.Format(New BooleanFormatter, "{0:On;Off}", False)
s = String.Format(New BooleanFormatter, "{0:On;Off}", True)
End Sub

End Class

Public Class BooleanFormatter
Implements ICustomFormatter
Implements IFormatProvider

#Region " Custom formatter support "

Public Function Format(ByVal formatString As String, ByVal arg As
Object, ByVal formatProvider As System.IFormatProvider) As String Implements
System.ICustomFormatter.Format

If formatString Is Nothing OrElse Not TypeOf arg Is Boolean Then

If TypeOf arg Is IFormattable Then
Return DirectCast(arg,
IFormattable).ToString(formatString, formatProvider)
Else
Return arg.ToString()
End If
End If
Dim sections() As String = formatString.Split(";"c)
Dim argBool As Boolean = DirectCast(arg, Boolean)
If argBool Then
Return sections(0)
Else
Return sections(1)
End If
End Function

#End Region

#Region " Format provider support "

Public Function GetFormat(ByVal formatType As System.Type) As Object
Implements System.IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function

#End Region

End Class

End Namespace

Note you can use the BooleanFormatter from String.Format and other methods
that accept a format specifier & a format string, however you cannot use it
from Boolean.ToString, as Boolean to not support IFormattable.

Hope this helps
Jay

"Eric Gofoth" <an*******@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Hello,

There doesn't appear to be an iformatprovider for to specify the format to convert a boolean to.
i.e. MyBool.ToString("YES/NO") does not work.

Any workarounds?

Nov 20 '05 #2

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
2
by: XmlAdoNewbie | last post by:
Hello All, I am trying to bind a date in the form of "20031218" to a datetime object. I first try to convert the string using Convert.ToDateTime("20031218") but i get an invalid date time...
0
by: Kolmi | last post by:
Has anyone come across a solution to this ? To format a value into an Sql Server sql string, I want to use the call Convert.ToString( value, new MyFormatProvider() ); Why I have to do this...
1
by: vooose | last post by:
Consider an application which has *many* references to DateTime.Parse(string); Is there a way, say when the application first starts up to force this method to use a specific IFormatProvider,...
10
by: Henri | last post by:
In java for instance there's a way to use booleans as objects and not as value types. I would like to do the same in VB.NET so that I can check if the boolean has been explicitely defined (is not...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
3
by: Que | last post by:
Hi The I wrote the following Code If Not txtInvoice.Text = "" Then txtInvoice.Text = Convert.ToInt32(txtInvoice.Text()).ToString("000000") End If
4
by: 0to60 | last post by:
I have to write to a fixed field file, and I have to format my numbers in a particular way. I have to front pad them with 0's, and I have to write the decimal value 3.14 as "314". So for example,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...

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.