Boolean String

How to create a line such as "You did it!" appear from boolean pass / fail instead of “True” or “False”? I can only ever find the ToString method, but all that does is change from True to True. I want to know how to output the string what I want.

+5
source share
6 answers

A bool is a type, just as a string is a type. You can never get the bool type to show “You did it!”, Because its bool - it can only be true or false.

You want to create a string based on a logical result. It looks like you need an if :

 string myString; if(myBool) // if true { myString = "You did it!"; } else { myString = "You didn't do it"; } 

as @juharr showed, there are ways to do this in smaller lines of code, but this is the logic that should happen.

As a side note, you see the ToString method on bool , because every type that inherits from System.Object inherits the ToString method, giving you a string representation of this object. This is not applicable for what you want to do here.

+7
source

If you use C # 3.0 or higher, you can also put this in the extension method for bool as follows:

 public static string ToDidItOrNotString(this bool b) { return b ? "You did it!" : "You did not do it."; } 

Then you can simply call it like this:

 string s = boolVal.ToDidItOrNotString(); 
+2
source

To display a string in your view when using data binding, you can use your own IValueConverter:

 public class BoolToStringConverter : IValueConverter { public string TrueString { get; set; } public string FalseString { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var boolValue = (bool)value; return boolValue ? TrueString : FalseString; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

XAML:

 <stackOverflow:BoolToStringConverter x:Key="BoolToStringConverter" TrueString="You did it!" FalseString="You didn't do it"/> <TextBlock Text="{Binding WasItDone, Converter={StaticResource BoolToStringConverter}}"/> 
0
source

I think creating the Extension Method is closer than you can get from what you want.

Create the BoolExtensions class:

 public static class BoolExtensions { public static string ToString(this bool boolean, string valueIfTrue, string valueIfFalse) { return boolean ? valueIfTrue : valueIfFalse; } } 

Then you can do this:

 static void Main(string[] args) { var trueBoolean = true; Console.WriteLine(trueBoolean.ToString("You did it!", "You didn't do it")); var falseBoolean = false; Console.WriteLine(falseBoolean.ToString("You did it!", "You didn't do it")); Console.ReadKey(); } 

Note that both the BoolExtensions class and the ToString method are marked as static

0
source

You cannot override the ToString method of a known type in C #, but you can create extension methods, if you use this line in many parts of your code, you can create a method for this:

 public static class Extensions { public static string ToStringCustom(this bool booleanValue) { if(booleanValue) return "You did it"; else return "You didn't do id"; } } 

and then when you use boolean variables, this method will be available to you, for example:

 Console.WriteLine(true.ToStringCustom()) 

it will print "You did it."

0
source

If you want to use this function in several places, you can create an extension method, for example:

 public static string YouDidIt(this bool value ) { if (value) return "You did it!"; return "You failed"; } 

Then you can use it like:

 bool a = true; string s = a.YouDidIt(); 
0
source

Source: https://habr.com/ru/post/1246263/


All Articles