|
An ordinary VB developer shares his own successes and failures |
|
| Home News Articles Resources Tips Downloads About me | ||
"Option Strict Off" considered harmfulDiscusses 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
( 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
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 Here is what happened:
The application's code contained a 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 FunctionThe 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
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
© 2003 - Palo Mraz
PS: Here you can find a simple VB.NET console application
that demonstrates the danger of the © Palo Mraz - June 11, 2003 |
||
|
|
||
| ©2003-2007 Palo Mraz. All Rights Reserved. See my 'new browser window' policy | ||