Checking element values in a Boolean array - C#

11,121

Solution 1

instead of using the array it would be much easier to simply exit the method as soon as an error is detected:

private void print_button_Click(object sender, EventArgs e) {
  if (authorbox.Text == "") {
    MessageBox.Show("Author field empty", "Required Entry");
    return;
  }

  if (titlebox.Text == "") {
    MessageBox.Show("Title field Empty", "Required Entry");
    return;
  }

  printPreviewDialog1.Document = printDocument1;
  printPreviewDialog1.ShowDialog();
}

Solution 2

If you are using .NET 3.5 you can use Any and All to see if any of the booleans are true, or if all of them are true:

if (checker.Any(x => x))

or:

if (checker.All(x => x))

Also, if you want an array of two booleans, you should use new bool[2] not new bool[1]. It would be easier to use a List<bool> though.

Solution 3

Apart from other things, you should say

  bool[] checker = new bool[2];

if you want an array consisting of 2 elements ;) In this particular case the array doesn't seem to make too much sense, because it obfuscates things a little bit. You could do the same thing with just one boolean variable.

Solution 4

Using boolean arrays to accumulate a single go/no-go value is overkill. There are more useful things you could play with to get the hang of arrays.

You're better off simply ANDing the results of your intermediate checks into a value and then checking that for true/false:

public bool CheckControls()
{
    bool pass = true;
    pass &= !string.IsNullOrEmpty(authorbox.Text));
    pass &= !string.IsNullOrEmpty(titlebox.Text));
    // if any of these are empty then pass is to false and stays that way.
    return pass;
}

If you need to keep track of which intermediate test failed, then consider using an integer and predefined constants of powers of two. Here you instead check for zero if all is well. This allows you to mask against the returned value and accumulate any combination of test results. As long as you have less than 32 (or 64) tests.

    int AUTHORBOX = 2;
    int TITLEBOX = 4;
    int ISBNBOX = 8;
    int PRICEBOX = 16;

    public int AlternateCheck()
    {
        int temp = 0;
        temp += string.IsNullOrEmpty(authorbox.Text) ? AUTHORBOX : 0;
        temp += string.IsNullOrEmpty(titlebox.Text) ? TITLEBOX : 0;
        temp += string.IsNullOrEmpty(isbnbox.Text) ? ISBNBOX : 0;
        temp += string.IsNullOrEmpty(pricebox.Text) ? PRICEBOX : 0;
        return temp;
    }

Solution 5

Well this is not the ideal way for error handling but you can use the .Contains() Method.

if (checker.Contains(false))
{
   // Do Something
}
else 
{
   printPreviewDialog1.Document = printDocument1;
   printPreviewDialog1.ShowDialog();
}
Share:
11,121
Matt
Author by

Matt

Updated on June 04, 2022

Comments

  • Matt
    Matt almost 2 years

    I'm writing some error checking and trying to make use of an boolean array to store true or false in the elements and then my final condition parses through the stored elements to determine if its all true in visual studio 2008. Theres probably a easier way to do the error checking, but might as well learn how to utilize an array. Here's what I have so far

    bool[] checker = new bool[1]; // declared array...I think
    
    private void print_button_Click(object sender, EventArgs e)
    {
      if (authorbox.Text == "")
      {
        MessageBox.Show("Author field empty", "Required Entry");
      }
      else
      {
        checker[0] = true; // assigning element to array correctly?
      }
    
      if (titlebox.Text == "")
      {
        MessageBox.Show("Title field Empty", "Required Entry");
      }
      else
      {
        checker[1] = true;
      }
    
      // The part I am having trouble with basically if any of my array elements are  
      // false don't execute printing. Else go ahead and print.
      if ()
      {
      }
      else 
      {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
      }
    }
    
  • NebuSoft
    NebuSoft about 14 years
    Quite right. I tend to make the assumption of 3.5 when using VS 2008 but that is not always the case. :)
  • user6170001
    user6170001 about 14 years
    Exiting immediately after detecting the first error causes usability problems. If both author and title are mandatory, and both are missing, you should tell the user that. Otherwise the user fixes one error only to be told "oh, but there's another error I didn't tell you about." (Admittedly the original code's sequence of multiple message boxes had bad usability too, but at least it gave the user all the info they needed.)
  • stmax
    stmax about 14 years
    having 10 messageboxes pop up would be pretty annoying. might be best to simply switch to using error providers (and disable the button on demand) or to atleast collect the error messages in a list and display them in a single messagebox... anyway, i wouldn't use bool arrays in either solution.
  • Paul Turner
    Paul Turner about 14 years
    If .NET 3.5 is available, this would be my preferred means, if only for being perfectly succinct.
  • Dan Tao
    Dan Tao about 14 years
    I realize that effectively/logically this will accomplish the same thing as Contains(x), but it seems kind of roundabout to me. Essentially what these methods will be doing is enumerating through checker and evaluating if ((checker[i] == true) == true)...
  • HTTP 410
    HTTP 410 about 11 years
    List<T>.Contains is not LINQ-related, and is available in version 2.0 upwards of the .NET framework.
  • HTTP 410
    HTTP 410 about 11 years
    But bool[].Contains is LINQ-related.