Convert a Boolean value to a Yes/No string????

  • Thread starter Thread starter Alex Stevens
  • Start date Start date
A

Alex Stevens

Hi All.

How can I convert a boolean value to a Yes/No String.
If I use bolValue.ToString I get True And False????

Thanks

Alex
 
How can I convert a boolean value to a Yes/No String.
If I use bolValue.ToString I get True And False????

Of coarse it returns true or false. Look in the dictionary:

http://dictionary.reference.com/search?q=boolean

"Of or relating to a data type or variable in a programming language that
can have one of two values, true or false."

If you want Yes/No, then you have to write your own function.

public class Library
{
public static string Convert(bool b)
{
if (b){return "Yes";}else{return "No";}
}
}

or use the overload of ToString(IFormatProvider)

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfSystemBooleanClassToStringTopic2.asp

Michael Lang, MCSD
 
If using C# you can use the conditional operator (the ?: sequence)

cond-expr ? expr1 : expr2

bool myvalue=false;
string example=myvalue?"Yes":"No";

If the result of evaluating the expression to the left of the ? (which
happens to be just the variable myvalue) is true it return the value between
the ? and the : (expr1) otherwise it returns the value after the : (expr2)

I use this all the time to translate true/false booleans to 0 and 1 integers
or "0" and "1" strings for storage.

bool myvalue=false;
int test=myvalue?1:0;

And the other way of course:

int test=1;
bool myvalue=(test==1);

Hope this helps
Tom
 
Hi Alex,

depending on the context in which your are using the bool type, you could
also implement your own YesNoBoolean struct like Boolean implements one;

or a more simple one like the DBBool sample is contained in the .NET
reference:
public struct DBBool {

// The three possible DBBool values.

public static readonly DBBool Null = new DBBool(0);

public static readonly DBBool False = new DBBool(-1);

public static readonly DBBool True = new DBBool(1);

// Private field that stores -1, 0, 1 for False, Null, True.

sbyte value;

// Private instance constructor. The value parameter must be -1, 0, or 1.

DBBool(int value) {

this.value = (sbyte)value;

}

// Properties to examine the value of a DBBool. Return true if this

// DBBool has the given value, false otherwise.

public bool IsNull { get { return value == 0; } }

public bool IsFalse { get { return value < 0; } }

public bool IsTrue { get { return value > 0; } }

// Implicit conversion from bool to DBBool. Maps true to DBBool.True and

// false to DBBool.False.

public static implicit operator DBBool(bool x) {

return x? True: False;

}

// Explicit conversion from DBBool to bool. Throws an exception if the

// given DBBool is Null, otherwise returns true or false.

public static explicit operator bool(DBBool x) {

if (x.value == 0) throw new InvalidOperationException();

return x.value > 0;

}

// Equality operator. Returns Null if either operand is Null, otherwise

// returns True or False.

public static DBBool operator ==(DBBool x, DBBool y) {

if (x.value == 0 || y.value == 0) return Null;

return x.value == y.value? True: False;

}

// Inequality operator. Returns Null if either operand is Null, otherwise

// returns True or False.

public static DBBool operator !=(DBBool x, DBBool y) {

if (x.value == 0 || y.value == 0) return Null;

return x.value != y.value? True: False;

}

// Logical negation operator. Returns True if the operand is False, Null

// if the operand is Null, or False if the operand is True.

public static DBBool operator !(DBBool x) {

return new DBBool(-x.value);

}



// Logical AND operator. Returns False if either operand is False,

// otherwise Null if either operand is Null, otherwise True.

public static DBBool operator &(DBBool x, DBBool y) {

return new DBBool(x.value < y.value? x.value: y.value);

}

// Logical OR operator. Returns True if either operand is True, otherwise

// Null if either operand is Null, otherwise False.

public static DBBool operator |(DBBool x, DBBool y) {

return new DBBool(x.value > y.value? x.value: y.value);

}

// Definitely true operator. Returns true if the operand is True, false

// otherwise.

public static bool operator true(DBBool x) {

return x.value > 0;

}

// Definitely false operator. Returns true if the operand is False, false

// otherwise.

public static bool operator false(DBBool x) {

return x.value < 0;

}

public override bool Equals(object obj) {

if (!(obj is DBBool)) return false;

return value == ((DBBool)obj).value;

}

public override int GetHashCode() {

return value;

}

public override string ToString() {

if (value > 0) return "DBBool.Yes";

if (value < 0) return "DBBool.No";

return "DBBool.Null";

}

}

//----------------------------------------

....

DBBool db = DBBool.Null;

DBBool db1 = false;

DBBool db2 = true;

Console.WriteLine("db = {0}, db1 = {1}, db2 = {2}", db, db1, db2);



I modified the ToStriug method to return DBBool.Yes/No

Regards, Walter
 
Back
Top