VB.NET 2.0: Option Strict in code-behind files

12 01 2008

Being a C#-leaning programmer in a VB.NET world most of the time, I find myself cringing a little when I find out most architects or developers on my projects are not enabling the Option Strict option in their projects. Option Strict causes a developer to be cognizant of the implicit casts they are putting into their code, adding another layer of protection against data loss and runtime errors. With just a little extra work, you can spend less time testing or tracking down such errors.

Anyway, enough rhetoric, there’s plenty of information online on what Option Strict provides for you. The reason I’m here today is to share a tip on how to enable Option Strict in your code-behind files in a VB.NET web application (or web site). You can enable this option of course in each page/code file, but I recommend making it a global project setting.

Unfortunately, the Microsoft KB article that addresses this doesn’t work. Those web.config settings do not work. However, after some Google-ing I found an ASP.NET forum post addressing this issue. Here’s what you need to add to your web.config:

<system.codedom>
    <compilers>
      <compiler compilerOptions ="/optionexplicit+ /optionstrict+" language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </compilers>
  </system.codedom>

Actions

Information

3 responses

24 06 2008
gevet

Option Strict is prevents program from automatic variable conversions, that is implicit data type conversions

http://vb.net-informations.com/language/vb.net_option_strict.htm

http://vb.net-informations.com/language/vb.net_languagebasics_tutorials.htm

24 10 2008
james

What about partial trust environments? I don’t think the above compilerOptions are allowed in those. Microsoft should have added something to the web.config file by now. Many people have complained about this since VS 2005 and now it still plagues VS 2008 SP1

9 01 2009
illiniguy

Sorry, james, I was away from my blog for awhile and never tried your scenario. If you have any update, lemme know. Interested to know. If I get a chance myself I’ll give it a try.

Leave a comment