tags:

views:

148

answers:

4

How can I check to see if the textbox is empty and then only divide by that number of int. As you can see now I will divide by 10 everytime so now I need help with error checking.

namespace Assignment1_White
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num1;
            int num2;
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num8;
            int num9;
            int num10;
            int answer;

            num1 = int.Parse(num100.Text);
            num2 = int.Parse(num200.Text);
            num3 = int.Parse(num300.Text);
            num4 = int.Parse(num400.Text);
            num5 = int.Parse(num500.Text);
            num6 = int.Parse(num600.Text);
            num7 = int.Parse(num700.Text);
            num8 = int.Parse(num800.Text);
            num9 = int.Parse(num900.Text);
            num10 = int.Parse(num1000.Text);



            answer = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 +num10)/10;
            MessageBox.Show(answer.ToString());
+6  A: 
string.IsNullOrEmpty(num100.Text)

This will return true if it is empty, and false if it isn't.

You can also use int.TryParse instead of int.Parse. If you use TryParse, you will also implicitly handle non-int values in your text boxes:

if(!int.TryParse(num100.Text, out num1))
    num1 = 0;
// And for the rest...

Edit

After Brian's comment, I realized you shouldn't divide by 10 if you have empty/invalid input for some of the boxes. Here is the block you should use instead:

int count = 0;
int num1 = 0;
// And the rest...

if(int.TryParse(num100.Text, out num1))
    count++;
// And the rest...

int answer = 0;
if(count > 0)
    answer = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)
        / count;
Merlyn Morgan-Graham
Note that you will need to keep track of how many textboxes were empty so that you know what to divide by. Maybe add `cnt--;` in the if statement or add an `else cnt++;` clause. Also, make sure to detect if none of the boxes are filled in, as the application will throw an exception for division by 0 if you don't detect this.
Brian
@randywhite30: I'd also suggest you cram all those text boxes into a list, and keep a single variable to `sum` variable, instead of `num1`, `num2` etc. Set `sum` to zero, then use a `foreach` to loop over the list, so you write the `sum = sum + int.TryParse(...)` logic only once.
Merlyn Morgan-Graham
@Brian: Good catch :) I was worried about the parsing method more than the count.
Merlyn Morgan-Graham
@Merlyn: Stuff like this makes me sad that C# doesn't handle control arrays the way VB6 did. It's easy enough to do it yourself, but support from the designer would've been nice.
Brian
@Brian: I don't know much about VB6 or control arrays, but at least the TryParse part is handled in WPF, even if the count isn't :)
Merlyn Morgan-Graham
@Merlyn: Basically, vb6 makes it very easy to create an array of controls. In C#/VB.Net, your choices are: A) Bypass the designer and make the array yourself. B) Take the controls you made in the designer and make your own list of them (probably what you were suggesting). In VB6, the controls will actually be created as an array under the hood on their own, and you can do stuff like loops or attaching an event to all of them at once. It can save a bit of work and makes for cleaner code when the controls are homogeneous, as is the OP's case.
Brian
@Brian: If you're using WPF, you can get a reference to container objects (like the grid or stack panel), grab all its children, and do the same thing you are suggesting. Dunno if you can in WinForms, but I wouldn't be surprised if you couldn't.
Merlyn Morgan-Graham
@Merlyn: You still have a division by 0 bug in your code if the user skips all 10 fields.
Brian
@Brian: ty. Fixed
Merlyn Morgan-Graham
+1  A: 

You could do something like:

int count = 0;

//For each textbox
if(num100.Text != "")
{
     num1 = int.Parse(num100.Text);
     count++;
}

Then divide by count

Brett
+1 for paying attention to `count`, which is the actual issue the OP was asking about.
Brian
A: 
private void button1_Click(object sender, EventArgs e)
{
    int answer;

    int num1 = !string.IsNullOrEmpty(num100.Text)? int.Parse(num100.Text):0;
    int num2 = !string.IsNullOrEmpty(num200.Text)? int.Parse(num200.Text):0;
    //etc...
    answer = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 +num10)/10;
    MessageBox.Show(answer.ToString());
Eton B.
I like this conceptually, but it will throw an exception if the text box has a non-int value.
Merlyn Morgan-Graham
A: 
int num1 = 0;

if(!String.IsNullEmpty(num1.Text) && (new Regex("^/d$").Match(num1.Text)))
{
     num1 = Int32.Parse(num1.Text);
}

do this for each text box, this will check for both Empty Text boxes and also non-numeric entries.

Kiran Bheemarti
I would really rather use `Int32.TryParse` than a `Regex`.
Brian
I agree to be more aggressive one can tap into Key Press event and disable entering anything but numbers.
Kiran Bheemarti