I thought it might be a good idea to have a class that handles all the keyboard stuff so i dont have to keep writing input code over and over i mean thats one of the pros i read about OOP, reusable code.
I have created a Class called KeyboardUtility and I have it returning number only input when the function is called.
I'm just wondering if im on the right track as usual, or whether this should be done another way.
I only have one function, but want to add like 1 to 4 menu, or y / n question or letters only etc etc
any ideas/advice would be accepted with arms wide open!!!
I have created a Class called KeyboardUtility and I have it returning number only input when the function is called.
I'm just wondering if im on the right track as usual, or whether this should be done another way.
I only have one function, but want to add like 1 to 4 menu, or y / n question or letters only etc etc
any ideas/advice would be accepted with arms wide open!!!
C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace FootballLeague
{
class KeyboardUtility
{
public static int NumberOnlyInput(string askForNumbers)
{
bool finishedEnteringNumbers = false;
string userInput;
int userNumberReturned;
while (!finishedEnteringNumbers)
{
Console.WriteLine(askForNumbers);
userInput = Console.ReadLine();
bool isNumeric = int.TryParse(userInput, out userNumberReturned);
if (isNumeric)
{
return userNumberReturned;
}
else
{
Console.WriteLine("That is not a valid number...");
}
}
return 0;
}
}
}