Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do Option Strict and Option Explicit do?

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).

like image 624
yretuta Avatar asked Mar 16 '10 13:03

yretuta


People also ask

What does Option Strict do?

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.

What is the meaning of Option Explicit and Option base?

Option Explicit makes the declaration of Variables Mandatory while Option Base used at module level to declare the default lower bound for array subscripts.

What does Option Strict do in VB?

VB.NET Option Strict [On | Off] Option Strict is prevents program from automatic variable conversions, that is implicit data type conversions . By default Option Strict is Off . From the following example you can understand the use of Option Strict.


2 Answers

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.

like image 114
TLiebe Avatar answered Sep 24 '22 07:09

TLiebe


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".

like image 20
Matt Wilko Avatar answered Sep 23 '22 07:09

Matt Wilko