Friday, July 29, 2005

Clever overloading

In C# you can not set default values for method parameters like in Delphi. So you can not make a method like this:

private int DoSomething(int Start, int End = -1);

The only way to 'simulate' this is using an overloaded method.

What is overloading?
Overloading gives you the possibility to name two methods the same, but they must have different parameters. So in a way you can simulate the default parameters.
In Delphi you must mark this methods as overloaded in C# you can write your methods without any keyword.

When using overloading for 'default' purposes it should be considered that if you don't do this clever you will be put up with two methods with almost te same code, so it is wise to follow the rule that the method with the fewest parameters calls the other one.

So in the DoSomething method:
(A very stupid function indeed :) )

private int DoSomething(int Start, int End);
{
if (End !=- 1)
return Start + End;
else
return Start + 1000;
}

private int DoSomething(int Start);
{
return DoSomething(Start, -1);
//So do not do this, you will end up with two same code paths:
// return Start + 1000;
}

No comments:

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...