I have an int property in my viewmodel that returns a custom class property. I check if the class reference is null, and in that case I want to return some empty value to the view in order to NOT show any value.
The code is like this:
public int percentage
{
get {
if (customClass != null)
{
return customClass.getInt();
}
else
{
return 0;
}
...
}
If you want to return a “no value” result, you should return
null. Since that’s not a valid value for anint, you have to change the type of the property to beint?instead:If you have not seen the syntax before,
int?is shorthand forSystem.Nullable<int>.Don’t forget that you can also transform the value of
Percentageto anything you like really from inside the view by defining an appropriateConverterfor the binding.