Methods taking variable number of args (part 2)

In my previous post, I talked about the choice I made about taking a variable number of parameters. At that time, I used a simple params object[], and the logic was built into the method (in the constructor of the underlying form in fact, but it is not relevant here).
The problem with this approach is how to handle parameters with the same type but different meanings. A very simple example is the caption and the text of the InformationBox. Both are strings but how to differenciate between the two. I had to force the first parameter to be the text, but it was beginning to look like the MessageBox.

For those who are familiar with C# 3.0, you may have already guessed what I could have used if .NET 3.5 had been available at that time. I could have used object initializers. It is a simple constructor syntax that allow the developer to specify only the properties he (or she) wants to define.
For example, assuming the Client class is already defined with two properties "Name" and "FirstName" :
var myClient = new Client { Name = "Blais", FirstName = "Johann" };

So basically, I could have changed the API a little to use this particular syntax. The main benefit would have been to throw away the "guessing part" of the Show method because the value would directly reference the property.
The main disadvantage is that (internally) the parameter validation would have taken place later in the process. The params approach allows the parameters to be validated on the spot and not when we start the process of showing the window. I am aware that this particular point is subject to discussion but as the only developer of the component, I tend to agree with myself. :)
I did not change this behavior in the last release because I did not want to introduce breaking changes in the API given the potentially important number of existing users.

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