alternative way to validate

R

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
 
S

Scott Seligman

=?Utf-8?B?cm9kY2hhcg==?= said:
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");
}
}
}
}
 
P

Peter Duniho

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
 
T

Tom P.

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

In my opinion, if you add a menu item you are gonna want to add code
to deal with the selection of that item. So you are going to be making
code edits in any case.

Unless you have other ways of dealing with user input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top