vendredi 25 septembre 2015

Validate if integer equals string

So im trying to simply check if my variable is entered as a string, I want the if statement to go through and not an unhandled exception...

Here's my code:

Console.Write("Input: ");
int i;
bool success = int.TryParse("", out i);

if (success)
{
    Console.WriteLine("Enter Integer!");
}

else
{
    i = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Output: ", i);
}

So what am I doing wrong here? Every time I am entering a string, I'm not getting to the if statement, only a crash!

Aucun commentaire:

Enregistrer un commentaire