Skip to main content
3 of 7
deleted 59 characters in body; edited tags; edited title
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

Is my coding technique progressing in terms of C# loops?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

Malachi
  • 29.1k
  • 11
  • 87
  • 188