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>




HttpCompileException on ASP.NET 2.0 Precompiled Site

29 09 2006

Hmmm, just ran across something interesting.  Twas trying to publish a precompiled site to a 3rd-party host for the first time, and kept getting: ‘Cannot convert type ‘ASP.login_aspx’ to ‘System.Web.UI.WebControls.Login”  At first I thought perhaps it was a permissions thing, I mean it’s worked fine at my day job when deploying on our networks.

As usual, Google is your friend.  Basically don’t name your pages (or perhaps just your Page classes) the same as controls in (at least) the System.Web namespace(s).  Read there for more info.  Renamed my Login.aspx to XXLogin.aspx and presto!