Home All Groups Group Topic Archive Search About

alternative way to validate

Author
1 Jul 2009 6:18 PM
rodchar
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

Author
1 Jul 2009 6:42 PM
Scott Seligman
=?Utf-8?B?cm9kY2hhcg==?= <rodc***@discussions.microsoft.com> wrote:
>
>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?

It's mostly a style option.  I prefer to do something like this to
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"
Are all your drivers up to date? click for free checkup

Author
1 Jul 2009 6:45 PM
Peter Duniho
On Wed, 01 Jul 2009 11:18:01 -0700, rodchar 
<rodc***@discussions.microsoft.com> wrote:

Show quoteHide quote
> 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?

Personally, I wouldn't bother with a validate method at all.  I'd just 
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
Author
1 Jul 2009 7:09 PM
rodchar
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

Bookmark and Share