is there some way of return null if it can't parse a string to int?
with:
public .... , string? categoryID)
{
int.TryParse(categoryID, out categoryID);
getting "cannot convert from 'out string' to 'out int'
what to do?
EDIT:
No longer relevant because of asp.net constraints is the way to solve problem
/M
TryParse(String, Int32) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
TryParse method converts a string value to a corresponding 32-bit signed integer value data type. It returns a Boolean value True , if conversion successful and False , if conversion failed. In case of failed conversion, it doesn't throw any exception and 0 is assigned to the out variable.
TryParse method returns false i.e. a Boolean value, whereas int. Parse returns an exception.
When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. But what happens if the passed string itself is a string representation of '0'. So the TryParse will return zero.
How about this?
public int? ParseToNull(string categoryId)
{
int id;
return int.TryParse(categoryId, out id) ? (int?)id : null;
}
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