I use the following code to validate income numbers from ajax calls:
public Tuple<bool, int> ValidateInt(string TheCandidateInt)
{
int TheInt = 0;
if (Int32.TryParse(TheCandidateInt, out TheInt))
{
return new Tuple<bool, int>(true, TheInt);
}
else
{
return new Tuple<bool, int>(false, 0);
}
}
public Tuple<bool, short> ValidateShort(string TheCandidateShort)
{
short TheShort = 0;
if (Int16.TryParse(TheCandidateShort, out TheShort))
{
return new Tuple<bool, short>(true, TheShort);
}
else
{
return new Tuple<bool, short>(false, 0);
}
}
I also have the same types of function for Byte
and Short
. As you can see, I pass in a string and the return is a Tuple where Item1 is a boolean and Item2 is the value.
Is there a way to change these 4 methods into 1 and somehow factor out the data type of the parsing?
Thanks.