Thomas Alva Edison's light bulb VBInfoZine Home
   An ordinary VB developer shares his own successes and failures
   FREE to registered subscribers.  

"Option Strict Off" considered harmful

Discusses the dangerous implications of using the 'Option Strict Off' setting in Visual Basic .NET applications.
The other day, my buddy developer, Andy, sent me a note that he has checked-in a new version of a web application we built for one of our clients. I was responsible for testing the update and deploying it at the client's site. The application was a simple 'query-by-form' application, built with ASP.NET and Visual Basic .NET. (We used .NET framework 1.0 at that time, but I have verified that the issues discussed in this article apply to version 1.1 as well.)

The application allowed the user to enter some search criteria, queried a database and displayed some useful statistical information about the company's HelpDesk utilization.

Because our client was (and still is:-) a multinational company, we had to provide the application in Slovak, German and English language version. The user could change the language at runtime by simply clicking a particular flag icon in the browser window. The language chosen was stored in the ASP.NET session state (System.Web.SessionState.HttpSessionState.Item).

The statistical information that was produced by the application was displayed in a table and a couple of pie charts. The pie charts were generated on the fly by a code we have taken from the web and modified slightly.

Initially, the color for the pie chart's segments was hard coded into the application. It was implausible that the customer will like Andy's choice of colors though, so I told him to make the colors configurable through the web.config file. That was the feature Andy checked-in and I was supposed to test.

So far so good. I fired up the application, did some test inputs, analyzed the display...everything went fine. Knowing that ASP.NET monitors the web.config file, I changed a few color entries in the file and clicked the refresh button on my browser, eager to see my colors in the pie charts. But instead of my marvelous colors, I was "blessed" with an error message saying that "System.Reflection.AmbiguousMatchException: No accessible overloaded 'Res.GetString' is most specific for these arguments..."

Here is what happened:

The application's code contained a Res class, which had two shared, overloaded GetString methods like this:

Public Shared Function GetString(ByVal Session As HttpSessionState, ByVal id As String) As String
  Return GetString(Session.Item(LanguageKey), id)
End Function

Public Shared Function GetString(ByVal lang As String, ByVal id As String) As String
  ...    
End Function
The former overloaded function calls the latter, passing it the language key retrieved from the session state collection. When I modified the web.config file, ASP.NET presumably threw away the session state. As a result, the Session.Item(LanguageKey) statement returned null reference--Nothing. Because Nothing is untyped compatible with the Object type, the VB runtime could not choose which of the two GetString versions to call and generated the ambiguous match exception. Meet Option Strict Off, the Visual Studio's default setting for all VB.NET projects!

If the Option Strict On option were active, the above error would be caught at compile-time instead of runtime. This is because Option Strict Off generates late bound IL code for cases like this, which not only provides room for hard-to-find runtime errors, but also hurts performance. For me, those are sufficient reasons to make Option Strict On the default setting and use the "Off" option only when absolutely necessary. What about you?

I encourage you to go to the Visual Studio's Tools | Options menu, click the Projects \ VB Defaults folder and set Option Strict to On. You might then spend a bit more time writing CType-s or DirectCast-s, but you will avoid spending much more time tracking down some mysterious runtime errors reported to you by your angry users.

© 2003 - Palo Mraz

PS: Here you can find a simple VB.NET console application that demonstrates the danger of the Option Strict Off setting. Homework: Open the 0002-OptionStrict.exe file in ILDASM and look at the Startup.Main code--late binding in action!

© Palo Mraz - June 11, 2003

 ©2003-2007 Palo Mraz. All Rights Reserved.   See my 'new browser window' policy