Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

Something IsMissing - A Short Story

Here's a short story with an "Optional" twist and a surprising moral.

Flashback

VB6 supports optional parameters. The VB6 compiler is happy to see these parameters without the default value. We all know. In VB6, we could also check whether an optional parameter was passed by the caller or not of using the IsMissing method. But this method worked only on Variants. Why? Because, the other types of variables where automatically initialized to their default values. So, there is no way of checking whether the value passed is the default value or actual value.

Present day

Variants are disbanded from VB.NET. So, its buddy, the IsMissing method was also pushed out. Our short story does not end here. Here's the twist. VB.NET also supports optional parameters. We all know. VB.NET however does not support optional parameters without default values. We all know this too. What we may not know is C# (and possibly other languages that target the CLR) does not allow optional arguments. So, how does one program written with C# consume a method written in VB.NET declared with default parameters.

The answer is - That patient programmer must pass System.Reflection.Missing.Value to force the default value to be taken. Ofcource, this has to be done for all optional arguments in the method call and hence, can be really frustrating for a C# dev.

Moral of the storty

It is always better to use overloading than to use optional arguments. So, VB6 devs, please come out of your "Optional" habit. If you are addicted to Optional and are ticklish about overloading, then try limiting that addiction to Internal classes only ( Ahem…I mean Friends!)

 

Posted: Tue, Jan 27 2004 6:52 by Manoj G | with 3 comment(s)
Filed under:

Comments

Manoj G said:

It is true that you can pass System.Reflection.Missing.Value to force the default value to be taken, but ONLY if the type of the optional argument is 'object'. If the type is for example 'string', you realy have a problem calling that method from c#. The only solution I have found is to wrap the methods in Visual Basic witout using optional arguments. This is really frustrating.
# March 10, 2004 5:00 PM

Manoj G said:

very well written!
# April 22, 2005 6:45 AM

Darrin said:

This article's attitude is indicative of the overall contempt that C/C++/C# developers had for VB6, even though it filled a need for quick business application development. (where requirements change constantly... hello, real world)

Thus, we ended up with lack of path from VB6 to VB.NET, all because a bunch of eggheads at Microsoft thought they new better than the marketplace!

E.g. Optional parameters

Ok, its somehow better that I write a dozen overloaded methods when a single method with Optional parameters would've done... VB.NET creates the overloads behind the scenes for you.

Why do you C# guys like writing so much code?  I can't figure it out?

# December 21, 2007 12:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)