Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bool? to Yes/No/null string

Tags:

c#

asp.net

i have an object that as a parameter bool?

i need to show it on a repeater.

<%# Eval("LS_Body_Connection") %>

i create a class

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

how to use it on the repeater?

i try

((bool)Eval("LS_Body_Connection")).ToYesNoString()

but it's not recognaize

'bool' does not contain a definition for 'ToYesNoString' and no extension method 'ToYesNoString' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)

in the cs of the page i can call the extension method

when i add

<%@ Import Namespace="IDD.App_Code" %>

i get The call is ambiguous between the following methods or properties: 'IDD.App_Code.BooleanExtensions.ToYesNoString(bool?)' and 'IDD.App_Code.BooleanExtensions.ToYesNoString(bool?)'

like image 581
eyalb Avatar asked Apr 19 '26 23:04

eyalb


1 Answers

try ((bool)Eval("LS_Body_Connection")).ToYesNoString()

And you need to import the namespace like below as Claudio's Answer

<%@ Import Namespace="My.Namespace.Containing.MyExtensions.Class" %>
like image 171
Damith Avatar answered Apr 22 '26 11:04

Damith



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!