What do Option Strict and Option Explicit do?
Asked Answered
S

3

56

I saw this post:

Typos… Just use option strict and explicit please.. during one software development project, which I was on as a consultant, they were getting ridiculous amounts of errors everywhere… turned out the developer couldn’t spell and would declare variables with incorrect spelling.. no big deal, until you use the correct spelling when you’re assigning a value to it… and you had option explicit off. Ouch to them…"

What is Option Strict and Option Explicit anyway? I have googled it up but can't get the idea (because mostly it's Visual Basic, I'm doing PHP).

Saltcellar answered 16/3, 2010 at 13:15 Comment(2)
Option Explicit is perfectly described in the other answers. I just wanted to let people know that Option Strict does not seem to be supported any more. See : msdn.microsoft.com/en-us/library/aa338155(v=vs.60).aspxHepsibah
@Olivier: option strict is still with us, the way back machine shows the page you referenced not having an option strict listed for a 2013 crawl, but that must have simply been an over sight, it’s been there all along, and there’s no sign of it going away.Koel
B
75

Option Explicit means that all variables must be declared. See here. Without this, you can accidentally declare a new variable just by misspelling another variable name. This is one of those things that cause a lot of grief as you're trying to debug VB programs and figure out why your program isn't working properly. In my opinion, this shouldn't even be an option - it should always be on.

Option Strict "restricts implicit data type conversions to only widening conversions". See here. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.

Brume answered 16/3, 2010 at 13:20 Comment(2)
Option Explicit off for shorter programs saves the development time and energy of thinking up and typing in data types.Hallah
@DougNull No, Option Explicit has nothing to do with data types. Dim could always be used to declare a variable without specifying its type. With Option Explicit off you may additionally declare a variable by simply starting to use it. So if you misspell a previous variable, you're declaring new one: that's evil.Calandracalandria
M
38

TL;DR

Option Strict and Option Explicit help you to catch potential and actual errors at design time, rather than your code compiling and failing at runtime. You should switch both On.

Option Strict and Option Explicit are Off by default. To switch them on:

Option Strict Tools -> Options -> Projects and Solutions -> VB defaults -> Option Strict. Set it to On.

Option Explicit Tools -> Options -> Editor -> Require Variable Declaration. tick it.

Option Explicit

With Option Explicit Off you don't have to declare (Dim) a variable before using it:

a = 123 'a is automatically declared as an Integer

This becomes dangerous when you declare a variable in one place and think you are using it later but mis-type it:

Dim counter As Integer = 0
'some lines later...
countr = 55 'This creates a new variable called countr 

Or even worse, you assign a value to a variable that you think is in scope, but it isn't and you end up declaring a new variable with the same name but differing scope.

With a lot of code or long methods these can be easy to miss so you should always switch it on to prevent these sorts of issues.

Option Strict

With Option Strict Off you can implicitly convert a datatype to a narrowing type with no error:

Dim d As Double = 999.99
Dim s As Single = d 'No error with Option Strict Off

For these cases Option Strict serves as a warning to the developer to make sure that the double value should never exceed Single.MaxValue.

You can also assign an Enum to the incorrect value with no error. The following is a real example of this:

enter image description here

The variable should have been set to EOpticalCalStates.FAILED (24), in fact it sets the State to a value of 4 which is equivalent to EOpticalCalStates.ALI_HOR.

Something like this is not easy to spot.

Therefore you should always have Option Strict on by default. This setting should have been set on as default, but Microsoft decided to leave it off to increase backwards compatibility (which with hindsight was a mistake IMO).


If you have started a project before setting the default for new projects, you will need to use:

"Project" menu -> " Properties..." item -> "Compile" tab -> set "Option strict" to "On".

Maundy answered 1/5, 2015 at 10:12 Comment(1)
Option Explicit is on by default. support.microsoft.com/en-us/help/311329/…Calandracalandria
D
6

Find details here: http://support.microsoft.com/kb/311329

The Option Explicit statement

By default, the Visual Basic .NET or Visual Basic compiler enforces explicit variable declaration, which requires that you declare every variable before you use it. To change this default behavior, see the Change the Default Project Values section.

The Option Strict statement

By default, the Visual Basic .NET or Visual Basic compiler does not enforce strict data typing. To change this default behavior, see the Change the Default Project Values section.

Drawstring answered 16/3, 2010 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.