I have nullable int[] and string.
string someStr = "123";
int? siteNumber = null;
string siteName= null;
At some point I need to check if a string is number.
For this purpose I tryed this:
if (int.TryParse(someStr, out siteNumber))
{ }
else
siteName = siteDescription;
But because siteNumber is nullable I get this error:
cannot convert from 'out int?' to 'out int'
How can I check if string is number and if it is I need to assign it to nullable int?
You can write your custom method for doing this.
public int? TryParseNullableValues(string val)
{
int outValue;
return int.TryParse(val, out outValue) ? (int?)outValue : null;
}
You could use an intermediate variable because the out parameters types should match fully.
int siteNumberTemp;
if (int.TryParse(someStr, out siteNumberTemp))
{
siteNumber = siteNumberTemp;
}
else
siteName = siteDescription;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With