mercredi 28 juin 2017

Executing an if statement or try/catch based on whether or not the input is a string/int

I am trying to create an if statement or try/catch that will hopefully execute under the condition that the input received in the console application (that I am converting to Int32) is NOT an integer. This is the code snippet I have been using:

            Console.WriteLine("What year was your car made? [MUST BE IN YEAR FORMAT]");
            caryearchk = Console.ReadLine();

            if (caryearchk.GetType() != int) // Can't figure out a way to return a bool through .GetType()
            {
                Console.Write("Error. you MUST submit this in an integer form. The application will now close.");
                Console.ReadLine();
            }
            else
            {              
            }
            caryear = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("What is the drivetrain? [ FWD /// RWD /// AWD /// 4WD ]");
            cartrain = Console.ReadLine();
        }
        Car car = new Car(carmake, 

If there are any other/more efficient ways to go about this I would much rather prefer that. I am eager to know many different ways to solve basic problems like this

Aucun commentaire:

Enregistrer un commentaire