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

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>