Methods taking variable number of args (part 1)

A few months ago, I started a small project on Codeplex, its goal was simple : providing an alternative to the default Windows Forms MessageBox. One of the things that annoyed me with the MessageBox was to be forced to provide values for parameters I did not want to change.
Here is a little example :
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);

Clearly in this case, I should not have to specify the third parameter since it is already the default value.

So basically, I needed to create a method (Show in this case) that take a variable number of parameters. The problem was with the types of the parameters, I have a whole lot of different types.

At that time, I had two ways to achieve that :
  • Providing an overload of Show for each combination of parameters.
  • Using params and the only type shared by all the parameters : object.
The first one is nice when you have two parameters, but in InformationBox we are now dealing with up to 25 parameters at the same time (note : the number of possibilities is 29 digits long).
So I went for the second solution and this is the currently implemented in the 0.8 release. What I do is simply iterate over every parameter and affect it to the corresponding property based on its type.

Next time we'll see how I would have achieved my goal by using the C# 3.0 features.

Comments

Popular posts from this blog

Create a draft release and start it using the TFS REST API

Adding a delay before processing Textbox events

Change the deployment URL of a ClickOnce application