the problem I’m having is to validate the input means putting it in a try catch which then wont pass the variable through and I’m getting this error:
Use of unassigned local variable ‘MainMenuSelection’
I’ve validated using this method before but for some reason it’s not working now, please help
//Take the menu selection
try
{
mainMenuSelection = byte.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Please enter a valid selection");
}
switch (mainMenuSelection) //Where error is shown
Obviously user can input anything which would not be parsed as a single
byte. Try out using Byte.TryParse() method which does not generate exception and just return status flag.You can go further and add more analysis for an user input if needed:
Also perhaps you just need a single character not a byte?
Then just do: