11

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
        {
        }
    }
4

11 回答 11

17
int percent = 0
if (max != 0) percent = (100*position) / max
于 2008-10-01T22:57:08.363 回答
9

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.

于 2008-10-01T22:57:30.393 回答
9
  • 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.

于 2008-10-01T22:57:53.060 回答
3

Check for zero.

if ( max == 0 ) {
    txt = "0%";
} else {
    // Do the other stuff....
于 2008-10-01T22:57:25.837 回答
2

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.

于 2008-10-01T22:58:29.047 回答
1

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
于 2008-10-01T23:00:53.270 回答
0

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%.

于 2008-10-01T22:59:15.893 回答
0

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.

于 2008-10-01T22:59:35.363 回答
0

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.

于 2008-10-01T22:59:43.163 回答
0

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 *

于 2008-10-01T23:00:25.407 回答
0

您可以使用三元运算符。

int percent = max != 0 ? (100 * position) / max : 0;

这意味着当 max 不等于 0 时,要执行计算。如果它等于 0,那么它将百分比设置为 0。

于 2021-03-10T01:37:25.773 回答