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
First of all, why are you trying to parse a string to an int and stick the result back into a string?
The method signature is
so you have to give a variable of type
int
as second argument. This also means that you won't getnull
if parsing fails, instead the method will simply returnfalse
. But you can easily piece that together:Here's a proper use of
Int32.TryParse
:How about this?
Int is a value type which means there is no such thing as a null int. So no, TryParse will never alter the out parameter so that it is null.
But the problem you're having is you're passing a string to the out parameter of TryParse when its expecting an integer.
You need something like this...
Simplest and one-liner...
It works 'cause it's evaluated left to right. Null not so easy.
TryParse
will return false if the string can't be parsed. You can use this fact to return either the parsed value, or null. Anyway I guess that you are intending to returnint?
from your method, then it would be something like this: