Click here to Skip to main content
14,331,729 members
Rate this:
Please Sign up or sign in to vote.
See more:
i keep getting this error attempt to divide by zero.

code

private void tbLayers_TextChanged(object sender, EventArgs e)
        {
            int SqIn, comBo;

            int.TryParse(tbSqIn.Text, out SqIn);
            int.TryParse(cbPalett.Text, out comBo);

            tbTotesPerLayers.Text = (comBo / SqIn).ToString();


What I have tried:

combo / ((SqIn == 0) ? 0 : SqIn)
Posted
Updated 2-Feb-17 17:13pm
Comments
NotPolitcallyCorrect 2-Feb-17 13:34pm
   
Why don't you simply check if SqIn is zero and then not try and divide by zero?
Richard MacCutchan 2-Feb-17 13:37pm
   
That expression is wrong. If SqIn is zero you try to divide by zero, you should divide by 1.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Both
... (comBo / SqIn);

and
...combo / ((SqIn == 0) ? 0 : SqIn);

produce exactly the same result, second form os just more complicated.

The right question is "Do you expect SqIn to be zero ? or not ?"
If you expect SqIn to be zero, you must check it before trying to divide.

In
int.TryParse(tbSqIn.Text, out SqIn);

You don't check if TryParsev completed or not.
   
Comments
ZurdoDev 2-Feb-17 16:00pm
   
TryParse will be true and SqIn will have 0 in the case that they typed 0 into the textbox.
Rate this:
Please Sign up or sign in to vote.

Solution 1

As mentioned in the comments, you need to check for zero first. In the what I have tried section you wrote code that if SqIn is 0 then divide by 0. The statement is redundant and does nothing.

A trick you might be able to use is something like:
combo / (SqIn + 0.0000000001)


What this does is makes it so that divide by zero most likely won't ever happen and if you don't need great accuracy in the math, this is a workaround.

Or, just check for 0. Simple.
   
Comments
0x01AA 2-Feb-17 13:55pm
   
I don't think "+ 0.0000000001" helps.
a.) OP calculate with int
b.) Depending on nominator also + 0.0000000001 can end in Problem (I think)...not sure in this Point
Philippe Mori 2-Feb-17 17:12pm
   
And then the user (or the QA) decide to try to test -0.0000000001 and it crash again. In that case, normally you will write a custom string ("", "-", "n/a",...) instead and at some point do validation and show that the value are not valid to the user.
ZurdoDev 2-Feb-17 13:58pm
   
A. Pretty sure the compiler still allows it. I mostly do this trick in SQL to avoid writing a case statement.
B. Yes, but it seems very unlikely that it would ever happen.

As mentioned, checking for 0 is still best option.
0x01AA 2-Feb-17 14:00pm
   
convinced +5
Rate this:
Please Sign up or sign in to vote.

Solution 4

If SqIn is 0, just return combo or whatever you want.

tbTotesPerLayers.Text = SqIn == 0 : combo.ToString() ? (comBo / SqIn).ToString();
   
Comments
ZurdoDev 2-Feb-17 16:02pm
   
combo.ToString() would be the name of the control in the .net library. What good would that be in a math problem?

The real question is, what does the user want to have happen when it is 0. Hopefully just an error message.
Ramza360 6-Feb-17 11:40am
   
no combo is the integer... Look at the OP
ZurdoDev 6-Feb-17 11:43am
   
Ah, you are correct.
Philippe Mori 2-Feb-17 17:14pm
   
The idea is correct but not the displayed text. You either want to leave the field blank or display some text that clearly show that the value cannot be computed.
Rate this:
Please Sign up or sign in to vote.

Solution 2

combo / ((SqIn == 0) ? 0 : SqIn)
means
if (SqIn == 0)
  ... combo /0
else
  .... combo / SqIn


I think this should be enough to solve it.
   
Comments
Patrice T 2-Feb-17 14:25pm
   
"I think this should be enough to solve it."
are you sure it solve something ?
Philippe Mori 2-Feb-17 17:17pm
   
Even original code should have been enough... In fact, if OP used ?:, he should know what it does.
0x01AA 2-Feb-17 14:30pm
   
You are right :) It should be more "enough to recognize the Basic Problem". Solving a div by zero is very much depending on the Task.
0x01AA 2-Feb-17 17:20pm
   
But I think exactly this cryptic Trial is his only (at least his bigger) Problem *laugh*

uups, just recognized I should not write here, somebody likes to downvote me :(
Rate this:
Please Sign up or sign in to vote.

Solution 5

try this
string defaultText = "sqIn in zero";
         tbTotesPerLayers.Text = SqIn ==0 ? defaultText: (comBo / SqIn).ToString();
   
Comments
Member 12349103 4-Feb-17 10:33am
   
Thanks for your input the program runs fine within Visual Studio with no errors.When I run the exe I get the error, some of the boxes auto populate the data so as I type the tb populates 0 until I finish. Again runs fine within VS.All boxes have this type of format.
private void tbCasePerTier_TextChanged(object sender, EventArgs e)
        {
            int Height, TotesLayers;

            int.TryParse(tbHeight.Text, out Height);
            int.TryParse(tbTotesLayers.Text, out TotesLayers);

            string defaultText = "Height in zero";
            tbTotesPerLayers.Text = Height == 0 ? defaultText : (TotesLayers / Height).ToString();
Karthik_Mahalingam 5-Feb-17 0:08am
   
what was the error
Member 12349103 5-Feb-17 9:49am
   
Attempt to divide by zero.
Karthik_Mahalingam 5-Feb-17 22:25pm
   
then it might be from some other area, check with try catch block.
Member 12349103 6-Feb-17 18:33pm
   
Thanks guys been a lot of help this code worked for me how it helps others.
I used it in all on all the boxes.

 private void tbLayers_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int SqIn, comBo;


                if (string.IsNullOrEmpty(tbSqIn.Text) || string.IsNullOrEmpty(tbPallet.Text))
                    return;

                int.TryParse(tbSqIn.Text, out SqIn);
                int.TryParse(tbPallet.Text, out comBo);

                if (SqIn > 0)
                    tbTotesPerLayers.Text = (comBo / SqIn).ToString();
                else
                    tbTotesPerLayers.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Karthik_Mahalingam 6-Feb-17 23:58pm
   
good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100