Option Explicit, Strict and Compare

Option are strange for begineer programers. And when I first programed, it seemed to me that they are very hard to understand. First Options come in the Top on any any thing in your code except comment.
When understanding what each option do, you'll see : very easy and funny. Let's start by Option Explicit

Option Explicit :

Let say you have the following code

Dim x as integer
x = 2

* The code Compile in all cases *
But

x = 2

* The code doesn't compile but may compile *

Here the aim, When Option explicit is on it forces explicit declaration of all variables in your code, but if you disble it you may don't declare you variable.
By default Option Exlicit is On

Option Strict :
Option Strict On
Dim X as string = "5"
Dim Y as integer
Y = X * This code doesn't compile *
But if "Option Strict Off" the code compile.
So Option Strict Restricts implicit data type conversions and "IMPLICIT" so here's how to do when Option Strict is On
Option Strict On
Dim X as string = "5"
Dim Y as integer
Y = Cint(X) * This code compile *
Cint(object) make an explicit conversion to Integer which is obligatory when Option Strict is On

Option Compare (Text/Binary)
Binary
Optional. Results in string comparisons based on a sort order derived from the internal binary representations of the characters.
Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.

This is what we call (Case Sensitive) without entring you in big stories :
Option Compare Text : A = a (Equal)
Option Compare Binary : A # a (Not Equal)
Option Compare Text
msgbox (a=A) 'Return True
Option Compare Binary
msgbox (a=A) 'Return False

Hope this helped you ! If you have question leave a comment ;)

0 comments:

Post a Comment