views:

2127

answers:

12

Hi all,

C# novice here, when the int 'max' below is 0 I get a divide by zero error, I can see why this happens but how should I handle this when max is 0? position is also an int.

    private void SetProgressBar(string text, int position, int max)
    {
        try
        {
            int percent = (100 * position) / max; //when max is 0 bug hits
            string txt = text + String.Format(". {0}%", percent);
            SetStatus(txt);
        }
        catch
        {
        }
    }

Many thanks

+10  A: 
int percent = 0
if (max != 0) percent = (100*position) / max
Simon
@Simon: +1, could you edit your post to make that code? I don't quite have the rep yet to edit your post.
sixlettervariables
+3  A: 

Check for zero.

if ( max == 0 ) {
    txt = "0%";
} else {
    // Do the other stuff....
palehorse
+6  A: 

Well, that entirely depends on the behaviour you want. If the maximum value of your program bar is zero, is it full? Is it empty? This is a design choice, and when you've chosen, just test for max == 0 and deploy your answer.

Adam Wright
+1  A: 
  • You can throw an exception.
  • You can do int percent = ( max > 0 ) ? (100 * position) / max : 0;
  • You can choose to do nothing instead of assigning a value to percent.
  • many, many other things...

Depends on what you want.

Swati
+1  A: 

This is not a C# problem, it's a math problem. Division by zero is undefined. Have an if statement that checks whether max > 0 and only execute your division then.

Esteban Araya
A: 

Well, if max is zero, then there is no progress to be made. Try catching the exception where this is called. That is probably the place to decide whether there is a problem or if the progress bar should be set at zero or at 100%.

Marcin
A: 

I guess the root question is: Does it make sense to even call this function where max is '0'? If yes, then I'd add special handling to it i.e.:

if (max == 0) 
{
    //do special handling here
}
else
{
    //do normal code here
}

If 0 doesn't make sense, I'd investigate where it's coming from.

Bob King
A: 

You would need a guard clause which checks for max == 0.

private void SetProgressBar(string text, int position, int max)
{
    if(max == 0)
        return;
    int percent = (100 * position) / max; //when max is 0 bug hits
    string txt = text + String.Format(". {0}%", percent);
    SetStatus(txt);
}

You could also handle the Divide by Zero exception, as your sample showed, but it is generally more costly to handle exceptions then to set up checks for known bad values.

ckramer
A: 

If you are using this for a download, you'll probably want to show 0% as I assume max would == 0 in this case when you don't KNOW the file size yet.

int percent = 0;
if (max != 0)
    ...;

If you are using this for some other long task, I'd want to assume 100%

But also, since position can never be between 0 and -1, so you'll probably want to drop the 100 *

Dre
A: 

Convert your

int percent = (100 * position) / max;

into

int percent;
if (max != 0)
    percent = (100 * position) / max;
else
    percent = 100; // or whatever fits your needs
ΤΖΩΤΖΙΟΥ
A: 

Wow, thanks!

A: 

thank you for all

yelcat