How can I test an object to see if it is an int with a value of 3?

  • A+
Category:Languages

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?

 


public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {     return object.Equals(value, 3); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: