Hey guys, Swifty here, and i'd like to introduce a lot of you guys to a statement called Option Strict.
Option Strict is a relatively new concept that will disallow of certain, commonly used code structures and code segments. Essentially Option Strict enables
strong typing (wiki:
Strong and weak typing - Wikipedia, the free encyclopedia), in other words it calls for more restrictions and disallows of "lazy" or inefficient coding - to an extent.
So what exactly does this Option Strict limit?
To begin, Option Strict does not allow for undefined variables. For example, one cannot declare a variable in the following manner:
Also, Option Strict does not allow for certain implicit conversions, such is the case when an integer is assigned a value using a string format (which is surprisingly common). Examples:
Incorrect:
Code:
Dim a as integer = "1"
Correct:
Code:
Dim a as integer = 1
Note that forbidden implicit conversions aren't limited to integer -> string conversions. Infact, all conversions are forbidden except for those that are widening conversions (conversions that don't allow lose of data, for more information in widening conversion, visit the MS link:
Option Explicit and Option Strict in Visual Basic .NET and in Visual Basic). Additionally, conversions that lose accuracy either through dropped chars or rounding are permitted.
Also considered by Options Strict are instances of
late binding.
To begin this section of the thread, I would first like to note, that whenever possible, the developer should
always shoot for early binding, and never for late binding, I will elaborate now. Now, lets begin by defining what binding is. Binding i occurs whenever an instance of an object is assigned to an object variable is detected. There are fundamentally two kinds of bindings that are possible, early, and late. One of the main advantages of using early binding is efficiency.
Instances of both kinds of binding:
Early binding:
Code:
Dim my_info As System****.FileInfo
my_info = New System****.FileInfo("loc")
Late binding:
Code:
Dim ctrl As Object = sender.GetType()
Dim name_property As Object = type.GetProperty("Name")
There are a number of disadvantages associated with late binding.
For one, your program will debug and run significantly slower than applications applying early binding. This occurs as result of time spent determining the object type, something already defined in early binding. The IDE is great in that the JIT (just in time debugger) as well as the compiler can identify errors that run the potential of crashing your application.
This "catch" is only evident in early binding.
Finally, due to the simple fact that Intellisense doesn't work with string inputs, it most certainly won't work when identifying objects on runtime.
Almost forgot to mention how to enable Option Strict. As stated earlier, unlike Option Explicit (for another day), Option Strict by default is disabled. To enable it, Insert the following code at the top of your code:
Further, to always have it enabled, simply visit:
Tools -> Options -> Projects and Solutions -> VB Defaults -> Option Strict On
Thanks for reading guys! IF you have any
questions, additions, or corrections, feel free to comment and let me know.
Thanks guys!
Swifty