How to Generically Format a Boolean to a Yes/No String

How to generically format a boolean to a Yes/No string?

The framework itself does not provide this for you (as far as I know). Translating true/false into yes/no does not strike me as more common than other potential translations (such as on/off, checked/unchecked, read-only/read-write or whatever).

I imagine that the easiest way to encapsulate the behavior is to make an extension method that wraps the construct that you suggest yourself in your question:

public static class BooleanExtensions
{
public static string ToYesNoString(this bool value)
{
return value ? Resources.Yes : Resources.No;
}
}

Usage:

bool someValue = GetSomeValue();
Console.WriteLine(someValue.ToYesNoString());

How to display Yes/No instead of True/False for bool? in c#

You can't return a string if the return type is bool (or bool? in this case). You return a bool:

return result;

Notice, however, that you ask...

How to display Yes/No...

This code isn't displaying anything. This is a property on an object, not a UI component. In the UI you can display whatever you like using this property as a flag:

someObject.BuyerSampleSent ? "Yes" : "No"

Conversely, if you want a display-friendly message on the object itself (perhaps it's a view model?) then you can add a property for that message:

public string BuyerSampleSentMessage
{
get { return this.BuyerSampleSent ? "Yes" : "No"; }
}

Displaying bool value as Yes or No

Use ternary operator while storing your value in the object

rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"] ? "Yes" : "No";  

And make VideoOnDemand as string

public string VideoOnDemand { get; set; }

use the same approach for rest of the variables for which you need YES/ NO

How to convert boolean to string?

How about the ternary operator?

Console.WriteLine("My first name is  " + ime + 
". My last name is " + Last_name + "" +
". My job is " + job + ". My hobby is " + hobby +
". I am " + years + " years old" + ". Married: " + (married? "yes" : "no"));

You could likewise add a property to Podaci the renders this as a string:

public class Podaci
{
public string ime, lastName, job, hobby;
public int years;
public bool married;

public string MarriedString => married?"Yes":"No";

public void Write()
{
Console.WriteLine("My first name is " + ime +
". My last name is " + lastName + "" +
". My job is " + job + ". My hobby is " + hobby +
". I am " + years + " years old" + ". Married: " + MarriedString);
}
}

How to convert Boolean value to Yes/No while rendering the flexigrid table

You can supply a "preProcess" callback function to alter/format the data returned by the ajax call before it is displayed:

preProcess: function(data) {
$.each(data.rows, function(i, row) {
row.checked = row.checked ? 'Yes' : 'No';
});
return data;
},

Demo on JSFiddle


Note: I don't really see where the "preProcess" callback is documented other than the following note on the Flexigrid project page:

New preProcess API, allows you to modify or process data sent by
server before passing it to Flexigrid, allowing you to use your own
JSON format for example.

It seems, however, to work pretty much like the "dataFilter" callback of the jQuery .ajax function.

Replace text that shows up in an boolean attribute

Following should help you convert bool to Yes/No strings.

string.Format("{0:Yes;0;No}", value.GetHashCode());

Another option would be to use a simple condition

result = value ? "Yes" : "No";

Can .NET convert Yes & No to boolean without If?

If you think about it, "yes" cannot be converted to bool because it is a language and context specific string.

"Yes" is not synonymous with true (especially when your wife says it...!). For things like that you need to convert it yourself; "yes" means "true", "mmmm yeeessss" means "half true, half false, maybe", etc.

Changing bool value to word using ToString method

Instead of Workday.ToString(), you might try to print
Workday ? "yes" : "no" , which is a short if else statement which prints "yes" when Workday is True, "no" otherwise.
Hope this helps!

WPF convert boolean binding to a string like Yes or No

Like Clemens' comment says, you need to add the necessary XML namespace declaration, provide a non-generic type, and declare your converter object appropriately (it can either go in a dictionary, with x:Key..., or you can specify it inline).

The changes would look something like this (I've omitted all of the XAML that does not appear to directly relate to your question):

<Window x:Class="MyProject.Forms.Products"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MyProject.Forms"
Title="Products" Height="768" Width="1024">
<Grid>
<DataGrid ItemsSource="{Binding}">
<DataGrid.Resources>
<local:BoolToStringConverter x:Key="BooleanToStringConverter"
FalseValue="No" TrueValue="Yes" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding IsNew, Converter={StaticResource BooleanToStringConverter}"
Header="Is it new product(Yes/No)."/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

Note the xmlns:local... declaration added to the Window element, and the location of the <local:BoolToStringConverter.../> element in the DataGrid.Resources element.

For what it's worth, since the TrueValue and FalseValue properties are being set in the resource declaration, I would actually give the resource a more description key, like "BooleanToYesNoConverter".

I note that your C# example shows a class named BoolToValueConverter<T>, while the type name you use in the XAML is BoolToStringConverter. This is fine, as long as you fix the generic type issue by specializing your generic subclass. Which you can do easily:

class BoolToStringConverter : BoolToValueConverter<string> { }

With the above changes, everything you want will work fine. However, I would recommend a slightly different implementation for BoolToValueConverter<T>:

class BoolToValueConverter<T> : IValueConverter
{
public T TrueValue { get; set; }
public T FalseValue { get; set; }

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? FalseValue : ((bool)value ? TrueValue : FalseValue);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Note: this implementation precludes the use of "null" as the
// value for TrueValue. Probably not an issue in 99.94% of all cases,
// but something to consider, if one is looking to make a truly 100%
// general-purpose class here.
return value != null && EqualityComparer<T>.Default.Equals((T)value, TrueValue);
}
}

That is, use EqualityComparer<T>.Default.Equals() for your equality comparison. This can be slightly more efficient than using the virtual object.Equals() override, especially for value types (not an issue in your example, but could be in other scenarios).

How to Convert Boolean to String

Simplest solution:

$converted_res = $res ? 'true' : 'false';



Related Topics



Leave a reply



Submit