tags:

views:

455

answers:

6

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();
  }
}
A: 

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();
}
NebuSoft
+1  A: 

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.

Thomas Wanner
Ha, quite right. Didn't notice that part myself.
Dan Tao
A: 

I'm pretty sure the Contains method suggested by NebuSoft is a LINQ extension and therefore not available in .NET 2.0. You could however use the Array.IndexOf<T> method, like this:

if (Array.IndexOf<bool>(checker, false) != -1)
{
    // some element in the array is false
}
else
{
    // no false in the array
}

However, NebuSoft is right in asserting that this isn't the best approach. If you're curious to know more, I'll be happy to discuss it further.

Dan Tao
Quite right. I tend to make the assumption of 3.5 when using VS 2008 but that is not always the case. :)
NebuSoft
+3  A: 

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.

Mark Byers
If .NET 3.5 is available, this would be my preferred means, if only for being perfectly succinct.
Programming Hero
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)`...
Dan Tao
+1  A: 

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();
}
stmax
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.)
itowlson
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.
stmax
return ended up working nicely.
Matt
A: 

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;
    }
ebpower