Advertisement
NPSF3000

GetInput

Mar 27th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. /*
  2.   Console.WriteLine("Please select your initial bet [10,20,50]!");
  3.   bet = int.Parse(GetInput("10", "20", "50"));
  4. */
  5.  
  6. private static string GetInput(params string[] values)
  7. {
  8.     var match = GetInput(line => values.Count(v => v.ToLower() == line.ToLower()) == 1);
  9.     return values.First(vv => vv.ToLower() == match.ToLower());
  10. }
  11.  
  12. private static string GetInput(Func<string, bool> inRange)
  13. {
  14.     return GetInput(_ => _, inRange);
  15. }
  16.  
  17. /*
  18.   Console.WriteLine("Please Enter Your Age [5..105]");
  19.   playerCount = GetInput(s => int.Parse(s), i => i > 4 && i < 106);
  20. */
  21.  
  22. private static T GetInput<T>(Func<string, T> parseInput, Func<T, bool> inRange)
  23. {
  24.     while (true)
  25.         try
  26.         {
  27.             var line = Console.ReadLine();
  28.             T t;
  29.             t = parseInput(line);
  30.             if (!inRange(t)) continue;
  31.             return t;
  32.         }
  33.         catch { };
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement