Issue
I trying to display nullable object. When variable is coming null to show 0. My object is double? and when this object is null He not showing on display.
How can I set 0 on this object when coming null ?
My xaml code:
Label x:Name="SnowForecastFirstDay"
HorizontalTextAlignment="Center"
Grid.Row="2"
Grid.Column="4"
FontSize="15"
TextColor="White"
FontAttributes="Bold"/>
My C# code:
double? snowForecastFirstDay = weatherBindingData.WeatherDataForecastHourly.List[0].SnowForecast.SnowForecastValue;
if (snowForecastFirstDay == null)
{
SnowForecastFirstDay.Text = "SNOW: 0mm";
}
else
{
SnowForecastFirstDay.Text = "SNOW:" + weatherBindingData.WeatherDataForecastHourly.List[0].SnowForecast.SnowForecastValue.ToString() + "mm";
}
I try with this if/else statement but my label not displaying on the screen. how is the right way object with null value to appear on the display?
Solution
try the null coalescing operator.
double? snowForecastFirstDay = weatherBindingData.WeatherDataForecastHourly.List[0].SnowForecast.SnowForecastValue ?? 0;
SnowForecastFirstDay.Text = "SNOW:" + snowForecastFirstDay + "mm";
Answered By - mehmetx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.