array.exist help

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,

string[] validOptions = { "1", "2", "3", "4", "e" };

if (Array.Exists<string>(validOptions, delegate(string s) {
return s.Contains(userInput);}))
{

Console.WriteLine("Thanks for the valid option.");
Console.ReadKey();
return true;
}

when a user just presses Enter without typing in anything it is considered a
valid option when in fact string.empty is not in the list. Any ideas?

thanks,
rodchar
 
hey all,

            string[] validOptions =  { "1", "2", "3", "4", "e" };            

            if (Array.Exists<string>(validOptions, delegate(string s) {
return s.Contains(userInput);}))
            {

                Console.WriteLine("Thanks for the valid option.");
                Console.ReadKey();
                return true;
            }

when a user just presses Enter without typing in anything it is considered a
valid option when in fact string.empty is not in the list. Any ideas?

thanks,
rodchar

All strings Contain empty space, therefore if you look for empty space
in a string you will find it. And if you null out the userInput you
will get a Null Object Reference error.

Best suggestion I can think of is to check expressly for
String.IsNullOrEmpty() and return to the input method. Then, if the
input gets past that check you can see if the input is one of the
actual choices.

Tom P.
 
Hi,

Likely because the empty string is contained into all those strings... I
believe what you want is to find out an array entry that is *equal* to the
user input not that *contains* the user input...
 
thanks for the help"o wise one" :)
rod.

Patrice said:
Hi,

Likely because the empty string is contained into all those strings... I
believe what you want is to find out an array entry that is *equal* to the
user input not that *contains* the user input...

--
Patrice

rodchar said:
hey all,

string[] validOptions = { "1", "2", "3", "4", "e" };

if (Array.Exists<string>(validOptions, delegate(string s) {
return s.Contains(userInput);}))
{

Console.WriteLine("Thanks for the valid option.");
Console.ReadKey();
return true;
}

when a user just presses Enter without typing in anything it is considered
a
valid option when in fact string.empty is not in the list. Any ideas?

thanks,
rodchar
 
rodchar said:
hey all,

string[] validOptions = { "1", "2", "3", "4", "e" };

if (Array.Exists<string>(validOptions, delegate(string s) {
return s.Contains(userInput);}))
{

Console.WriteLine("Thanks for the valid option.");
Console.ReadKey();
return true;
}

A predicate is overkill for what you're doing. Use

Array.IndexOf(validOptions, userInput) >= 0
 
Back
Top