Click here to Skip to main content
Click here to Skip to main content

One guideline every VB.NET project should follow!

, 25 Feb 2012
Rate this:
Please Sign up or sign in to vote.
On the importance of Option Strict...
When creating a new VB project, Visual Studio will automatically put Option Strict Off.
What is Option Strict and why should it be on?
Having Option Strict[^] set to Off is VERY dangerous because it allows you to implicitly convert types to other types. Take for example the following code:
Dim numeric As Integer = "1"
Dim otherNumeric As Integer = "Hello"
The first assignment of a String to an Integer will succeed. The second will fail miserably... At run time! Because Option Strict is Off, Visual Studio will not give an error on these types of conversions. Option Strict On however DOES prevent this sort of thing. The only way the above code would pass when having Option Strict On is by explicitly casting, like follows:
Dim numeric As Integer = CType("1", Integer)
Dim otherNumeric As Integer = CType("Hello", Integer)
This will still generate an error at run time, but at least the programmer has been made very aware at design time that this case might go wrong! Having Option Strict Off is a sin punished by run time crashes during production while you are enjoying your weekend (not anymore!). If this ruined your weekend, you deserved it Poke tongue | ;-P
So how do you put Option Strict On? There are two methods. First, go to your Project Options and Click the Compile tab. Here you will see some ComboBoxes, one of them has the label "Option Strict:" above it. Make sure it's set to On. This will enable Option Strict for your entire Project! This option can be set to On by default by going to your Visual Studio Options (accessed from your Menu -> Tools -> Options...). In your Options screen, find the 'Projects and Solutions' options. Open this and then select 'VB Defaults'. This will show you the default settings for, among others, Option Strict. Make sure it's On. Every newly created Project will now have Option Strict On by default!
Another method is typing
Option Strict On

above your code file. This is especially handy when you have legacy software and setting Option Strict On on Project level will cause thousands of errors. At least your new code can benefit from design time errors and explicit casting!
So why would you ever want to have Option Strict Off? Well, in VB6 the Strict Off behaviour was default, so it is kind of a backward compatibility thing. All sorts of typecasting are done in the background (but do affect performance, and as mentioned, might throw unexpected Exceptions).
Second, Strict Off can be handy when using late binding. Consider, for example, the following code:
Dim someThirdPartyTool As Object = GetSomeObject
' Notice how DoSomething is NOT a method of Object!
This code is perfectly legal with Option Strict Off. In fact, you can call any 'non-existant' method on a variable of the type Object (also called late binding[^]). So imagine that GetSomeObject dynamically loads some third party tool in your application. What's worse, this third party tool has terrible versioning, backward compatibility and multiple versions cannot be installed side by side. GetSomeObject could simply load the version of the third party tool which just so happens to be installed on the current computer and return some Object which interacts with the third party tool. You can then call DoSomething on the returned Object even though DoSomething is NOT a method of Object (just make sure the method is supported by the third party tool). So the obvious upsides to this are that you do not even need to have the third party tool installed to write and compile this code (a customer calls and says they're having trouble with third party tool version 1, but you currently have version 2 installed which doesn't support that method anymore)! It will only throw Exceptions at run time when the tool is not found or the method is not supported. The obvious downside is that a minor spelling mistake will mess up your application good and you don't have type checking or intellisense. When writing this kind of code, make sure you have a detailed manual which has every object and method in the library described in great detail.
Even better, write a uniform Interface and create a different implementation for each version of the tool (more work, but strongly typed, intellisense is supported, design time errors etc.).
Another use for Option Strict Off is when working with COM Objects. I will not go into this discussion here, because I have no experience with this, but here[^] is a small blog post that's worth reading. Notice how working with COM can be achieved with Strict On too.
So while I do not think Option Strict Off is the best method to deal with any of these kind of situations, it might have its uses in some environments (the one having to maintain your code will want to kill you though, speaking from experience here Smile | :) ).
Some more reading:
Coding Horror: Option Strict and Option Explicit in VB.NET 2005[^]
Granular Late Binding: VB Whidbey Fun[^]
Option Strict Off Considered Harmful[^]
Why is this an issue in VB specifically? It's on by default in C#. Smile | :)
And why am I posting this? Because I see lots of new code that does not have Option Strict On and will almost certainly result in run time errors. Cry | :((
Happy coding!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sander Rossel
Software Developer
Netherlands Netherlands
Started programming in 2010 after completing a Bachelor Common Art and Cultural Sciences and a Master Media and Journalism. A decision I haven't come to regret!
A company hired me and they taught me the basics of VB.NET, WinForms and SQL Server.
At the end of that year I signed up for CodeProject and that was when my programming knowledge increased rapidly. Being around some of the best and most enthausiastic coders in the world certainly helps you develop your own skills.
After a while I enrolled in some IT courses at the Open University and I also got Microsoft Certified.
I am currently working to get a Bachelor degree in IT.
At my job I am programming in VB.NET, C# and SQL Server using WinForms and DevExpress controls.
Meanwhile I am trying to educate others through my (prize-winning) articles[^] here on CodeProject.

Comments and Discussions

GeneralReason for my vote of 5 nice one. Pinmembernikhi _singh26-Feb-12 17:45 
GeneralRe: Thanks :) PinmemberNaerling26-Feb-12 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 25 Feb 2012
Article Copyright 2012 by Sander Rossel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid