|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
alternative way to validatehey all,
private static bool Validate(string userInput) { string[] validOptions = { "1", "2", "3", "4", "E" }; return Array.Exists<string>(validOptions, delegate(string s) { return s.Equals(userInput.ToUpper()); }); } this validation routine is to my console application's menu option. i was just wondering if they are other ways to do this. because it smells a little like what if i put a new option in the menu but forget to add it to the array (unlikely but just curious) is this something to worry about? thanks, rodchar =?Utf-8?B?cm9kY2hhcg==?= <rodc***@discussions.microsoft.com> wrote:
> It's mostly a style option. I prefer to do something like this to>this validation routine is to my console application's menu option. i was >just wondering if they are other ways to do this. because it smells a little >like what if i put a new option in the menu but forget to add it to the array >(unlikely but just curious) is this something to worry about? make the code a little more self documenting, and so new functions don't need to modify existing functions to be implemented. class Command : Attribute { public string Value; public string Desc; public Command(string Value, string Desc) { this.Value = Value; this.Desc = Desc; } } class Program { [Command("1", "This is option 1")] static void Option1() { Console.WriteLine("You entered option 1"); } [Command("2", "This is option 2")] static void Option2() { Console.WriteLine("You entered option 2"); } [Command("3", "This is option 3")] static void Option3() { Console.WriteLine("You entered option 3"); } [Command("4", "This is option 4")] static void Option4() { Console.WriteLine("You entered option 4"); } [Command("E", "Exit")] static bool OptionE() { Console.WriteLine("You entered option E"); return false; } [Command("?", "Show help")] static void ShowHelp() { foreach (MethodInfo mi in typeof(Program).GetMethods( BindingFlags.Static | BindingFlags.NonPublic)) { foreach (object cur in mi.GetCustomAttributes(false)) { if (cur is Command) { Console.Write(((Command)cur).Value); Console.Write(" - "); Console.WriteLine(((Command)cur).Desc); break; } } } } static void Main(string[] args) { bool readInput = true; while (readInput) { Console.Write("] "); string input = Console.ReadLine(); bool found = false; foreach (MethodInfo mi in typeof(Program).GetMethods( BindingFlags.Static | BindingFlags.NonPublic)) { foreach (object cur in mi.GetCustomAttributes(false)) { if (cur is Command && string.Compare( ((Command)cur).Value, input, true) == 0) { object ret = mi.Invoke(null, null); if (ret is bool && ((bool)ret) == false) { readInput = false; } found = true; break; } } if (found) { break; } } if (!found) { Console.WriteLine("Unknown command"); } } } } -- --------- Scott Seligman <scott at <firstname> and michelle dot net> --------- The number of votes I cast is simply a reflection of how firmly I believe in his policies. -- Blackadder in Black Adder III:"Dish and Dishonesty" On Wed, 01 Jul 2009 11:18:01 -0700, rodchar
<rodc***@discussions.microsoft.com> wrote: Show quoteHide quote > hey all, Personally, I wouldn't bother with a validate method at all. I'd just > > private static bool Validate(string userInput) > { > string[] validOptions = { "1", "2", "3", "4", "E" }; > > return > Array.Exists<string>(validOptions, > delegate(string s) { return > s.Equals(userInput.ToUpper()); }); > } > > this validation routine is to my console application's menu option. i was > just wondering if they are other ways to do this. because it smells a > little > like what if i put a new option in the menu but forget to add it to the > array > (unlikely but just curious) is this something to worry about? write a switch statement to handle the input, and have the default case report an error (since none of the valid inputs would have matched). You won't forget to include the case for a new menu item because when you add that menu item the first thing you'll do is try to test the code attached to the menu item, and you won't be able to do that if you leave the case out of the switch for it. Pete thanks all for the help,
rod. Show quoteHide quote "rodchar" wrote: > hey all, > > private static bool Validate(string userInput) > { > string[] validOptions = { "1", "2", "3", "4", "E" }; > > return > Array.Exists<string>(validOptions, > delegate(string s) { return s.Equals(userInput.ToUpper()); }); > } > > this validation routine is to my console application's menu option. i was > just wondering if they are other ways to do this. because it smells a little > like what if i put a new option in the menu but forget to add it to the array > (unlikely but just curious) is this something to worry about? > > thanks, > rodchar
Other interesting topics
exceptions to the rule
set location of Form2 based on Location of Form1? OT: missing posts Linq to Sql Exception Specified cast is not valid getting to know c# check console args A question on naming schemes - when frequent collissions with the BCLs are present C# Export Excel to client side Application configuration settings - Do I make global? How to find out what the server name is? |
|||||||||||||||||||||||