Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Boolean type into "ON" "OFF" using String.Format

Is there are any way to convert Boolean type into "ON"/"OFF" using string formatter, like:

Dim inpValue as Boolean
Dim outValue = String.Format("0:ON;OFF", inpValue)
' should show OFF as output

without code or IFormatProvider?

like image 935
volody Avatar asked Apr 10 '26 02:04

volody


2 Answers

What is wrong with:

Dim inpValue as Boolean
Dim outValue = If(inpValue, "ON", "OFF")
like image 57
vcsjones Avatar answered Apr 12 '26 14:04

vcsjones


Here's a crazy trick from another question but it's completely unreadable. I strongly advise you not to do this! If you are of nervous disposition, stop reading now.

String.Format("{0:ON;ON;OFF}", CDbl(inpValue)) 

This converts the Boolean to -1 or 0 and then uses a custom numeric format.

like image 39
MarkJ Avatar answered Apr 12 '26 16:04

MarkJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!