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
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 ;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
This code is perfectly legal with Option Strict Off. In fact, you can call any 'non-existant' method on a variable of the type
(also called late binding
]). So imagine that
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.
could simply load the version of the third party tool which just so happens to be installed on the current computer and return some
which interacts with the third party tool. You can then call
on the returned
is NOT a method of
(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 :)).
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#. :)
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. :((
I have been playing video games since mid-90's, when Windows 95 came out. I was around seven years old at the time. One thing led to another and I was exposed to some (scripting) languages including VB4, PHP, HTML and CSS.
It was not until summer 2010, after a Bachelor Common Art and Cultural Sciences and a Master Media and Journalism, that I decided to become a professional programmer. I was hired by a company and they taught me the basics of VB.NET and WinForms and using SQL Server database.
At the end of that same year I signed up on CodeProject and that is 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. I learned various Object Oriented Principles such as SOLID and Design Patterns.
I am still working in VB and WinForms using DevExpress Controls and the .NET Framework 4.0. I have experience in ADO.NET, Entity Framework 4.0, LINQ, TPL, WCF, and SQL.
I have also written some articles
] for CodeProject in both VB and C#. My second article, What not to do: Anti-Patterns and the Solutions
] became best VB article of the month april 2011, of which I am very proud
For those wondering what happened to my Bachelor Common Cultural Art and Sciences and Master Media and Journalism, I currently hold an MA title in Media and Journalism. I am not really doing anything with it, but I guess it helps me write those articles