2

I have C# method:

public class HeaderType1BoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = value as int;
        var ret = (s == 3);
        return !ret;
    }

}

What I need to do is to take that object (which will be an integer), check if its value is 3 and if so return true. Otherwise if it's null or not equal to 3 then I want to return false.

But I am having a problem as it says that

Error CS0077: The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type) (CS0077) (Japanese)

Can someone give me advice on how I can do this check?

2
  • 1
    Why you're using as if you know it's an int ? Also, the exception is straight forward you use as operator to do try to convert since we don't know what the object might be, weather it has a try to convert method there's the as operator, however int has a tryparse emthod, which you can use.
    – user7148391
    Commented Jul 22, 2018 at 12:08
  • 1
    What about using the is operator to check the type of value?
    – Progman
    Commented Jul 22, 2018 at 12:11

5 Answers 5

6
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return object.Equals(value, 3);
}
3
  • Hi Mark, Will this return false if the value is null?
    – Alan2
    Commented Jul 22, 2018 at 12:14
  • 1
    Yes. Be careful though, it will also return false if you pass in any non-null value that isn't an integer 3 (e.g. a double). Commented Jul 22, 2018 at 12:16
  • 1
    And it has to be Int32.
    – Anthony
    Commented Jul 22, 2018 at 12:18
3

You cannot use "as" for int, because it is value type. You can use nullable type with "as":

var s = value as int?;
1

You have done some wrong things in your code here is the fixed code to what do you want:-

public class HeaderType1BoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = 0;
        try{ s = (int)value; }catch(Exception e){ return false; }
        return s != 3;
    }

}
1

Two ways:

first is the most straight forward:

try
{
  string x = "text or int";
  int num = Convert.ToInt32(x);
  Console.WriteLine("this num is an int: " + num);
}
catch(Exception ex)
{
  Console.WriteLine("this num is not an int");
}

method 2 with GetType() method and typeof() method:

private bool isNumber(object p_Value)
    {
        try
        {
            if (int.Parse(p_Value.ToString()).GetType().Equals(typeof(int)))
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
1

try to use following tested code that eliminate your error.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (System.Convert.ToInt32(value)==3);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.