How do you declare a progress bar as an optional parameter?
Here is the function:
public int Factorial(int number, System.Windows.Forms.Label l, System.Windows.Forms.ProgressBar newprogressbar, int time=0)
{
....
}
The function has four parameters. Only int number and the label l should be mandatory.
time is already optional, but I don’t know how to make the new progressbar optional.
The function returns the factorial of a number and it’s uses a label to display it.
The progress bar should show the state of the stack and the time should be the speed at which the function works, but these two should be optional.
I have already done the function, but I still need to figure out how to make the progressbar optional.
The same way you declare any other parameter to be optional – you specify a default value. However, the default value has to be a constant, which for reference types other than
stringbasically meansnull:Personally I would change the design, however. Instead of making
Factorialknow about both “how to compute factorial values” and “how to display progress”, you could pass in a delegate:… then call that progress action on each iteration of your loop (which is what I assume you do with the progress bar).
That improves separation of concerns. If you don’t want to indicate progress in all cases, you could make
progressActiondefault tonull.Another option is to invert the control completely, and consider
Factorialas just a sequence of values – use an iterator block to do that easily:You can impose time restrictions (which I assume is what the
timeparameter is for?) separately, and this way the caller gets to work out how many times to iterate and what to do with the results. TheFactorialmethod only knows how to produce a sequence of factorial numbers.