The Basics - Option Strict
What is Option Strict?
In short, Option Strict is an optional feature in Visual Basic .NET that allows programmers to write better code.
Visual Basic .NET gives programmers a lot of options in how the code is written. The compiler that ultimately executes the VB.Net code is very flexible. There are some advantages and disadvantages of having such flexibility in a programming language, which is what we will go over.
Late Binding
In later lessons we will go over how to declare variables, but for now understand that Visual Basic .NET gives programmers the option to either implement early binding or late binding when declaring variables, functions, etc.
The difference between the two types of binding is that in early binding, the programmer specifies the exact data type of the variable where as in late binding the programmer declares a variable as an Object(which is the general catch all) and will allow programmers to omit the return type of a function.
The preferred method of programming in Visual Basic .NET is to use early binding for the following reasons:
- Predictability: Knowing the data type beforehand will ensure that the code will execute as expected.
- Better performance: There is no extra overhead during run-time to look up the object's data type.
- Ease of Development: Some development tools such as Visual Studios will provide autocompletion features (like IntelliSense) when object's are declared via early binding.
Whenever Option Strict is on, the Visual Basic.NET compiler will not allow for late binding.
Implicit Data Conversions
In later lessons we will go over how to convert data, but for now understand that Visual Basic .NET gives programmers the option to either implement implicit conversions or explicit conversions.
The difference between the two types of conversions is that in explicit conversions, the programmer specifies the how the data will be converted as opposed to implicit conversions the programmer allows the compiler to decide how the data will be converted.
The danger of implicit data conversions is that there is no guarentee that the values will be converted as expected, especially when converting numbers that have decimal places to numbers that do not have decimal places.
It should be the programmer's job to specify how values will be converted!
Whenever Option Strict is on, the Visual Basic.NET compiler will not allow for implicit data conversions.
Turn it On
The simplest method of turning Option Strict on, regardless of if the code is being developed in Visual Studios, DotNetFiddle, or any other development tool, is to turn it on via code.
To turn Option Strict on via code is to type the following line of code at the very top of the code file, before any auto-generated code:
Fiddle: Live Demo
Remarks
Many errors that occur when Option Strict is off are difficult to spot at first glance, it can potentially take a long time to find those errors too.
To save yourself a lot of time and to make yourself a better programmer, you should always keep Option Strict on when available.