0

I have a form with some fields on it:

folderNameLabel
folderTitle
folderDescription
folderCategory

Before the user will press OK, I want to check if all that fields are not =="". I wanted to make a function to receive an array as parameter and return a bool value, but I am not sure how to write it...

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

0
public bool Validate(List<string> parameters)
{
   foreach(string parameter in parameters)
   {
        if(String.IsNullOrEmpty(parameter))
        {
            return false;
        }
   }
   return true;
}
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
0

You could do the following:

private void btnOK_Click(object sender, EventArgs e)
{
    bool fieldsFilled = ValidateStrings(folderNameLabel.Text, 
                                        folderTitle.Text, 
                                        folderDescription.Text, 
                                        folderCategory.Text);
    if (fieldsFilled)
        DialogResult = DialogResult.OK;
    else
    {
        // Report errors
    }
}

private bool ValidateStrings(params string[] items)
{
    bool result = true;
    for (int i = 0; i < items.Length && result; i++)
        result &= !String.IsNullOrWhitespace(items[i]);

    return result;
}

Question: How do you tell the user which field he missed?

In your case, you could show a "You need to fill in all fields" message, but having only one optional field, that doesn't work anymore. This is why usually you don't do something like above.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • This is what I wanted, I never known how to make a function to take an indefinite number of parameters. Thank you so much! –  Sep 14 '12 at 09:04
  • 1
    That's short for `a = a && b;`. – Thorsten Dittmar Sep 14 '12 at 09:19
  • 1
    Actually, the loop can be optimized a bit - I'm modifying my answer accordingly. – Thorsten Dittmar Sep 14 '12 at 09:20
  • Bbut why is `for` better than `foreach`? –  Sep 14 '12 at 09:25
  • It's not only that for arrays `for` is quicker (http://stackoverflow.com/questions/365615/in-net-which-loop-runs-faster-for-or-foreach), but the loop now exits at the first occurrence of an empty string and does not check the other strings because the result would still be `false`. – Thorsten Dittmar Sep 14 '12 at 09:31
  • Yeah, I found out already that the previous function did not work properly... You know what I mean: "does not check the other strings because the result would still be false" –  Sep 14 '12 at 09:33
  • I forgot to click the button :)) –  Sep 14 '12 at 09:34
  • Should have worked fine - only looping too much. `true && !String.IsNullOrWhitespace(...) == true` as long as the string is actually not blank. If it is blank, the result comes out as `false`. After that `false && whatever == false`. – Thorsten Dittmar Sep 14 '12 at 09:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16682/discussion-between-victor-barbu-and-thorsten-dittmar) –  Sep 14 '12 at 09:36
  • It doesn't still work properly... If I fill just one filed of the ones sent as parameters, the function returns `true` –  Sep 14 '12 at 09:47
  • I just tried: `Console.WriteLine(ValidateStrings("Hello", ""));` which outputs `false` and `Console.WriteLine(ValidateStrings("Hello", "World"));`, which outputs `true`. Are you doing something else wrong? – Thorsten Dittmar Sep 14 '12 at 09:55
  • You were right, I corrected the some logical errors in my application –  Sep 14 '12 at 10:15