Classic VB - What is Option Explicit, and why should I use it?-VBForums
Results 1 to 2 of 2

Thread: Classic VB - What is Option Explicit, and why should I use it?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,039

    Classic VB - What is Option Explicit, and why should I use it?

    What does it mean?
    Putting the "Option Explicit" statement at the top of a code module (which includes forms/modules/classes/...) forces you to declare all variables that you have used in that module, using Dim or similar.

    If you try to run your code when a variable hasn't been declared, it will be highlighted, and you will get a clear error: "Variable not defined"


    Why should I use it?
    You are probably thinking "errors are bad, I don't want that!", but this is actually a very good error - as it tells you about problems that are hard to spot otherwise.

    Have a look at this code, can you see why it gives the wrong answers?
    vb Code:
    1. Dim MyVariable As Integer
    2.  
    3.   MyVariable = 10
    4.   MsgBox MyVariable  'should show 10
    5.  
    6.   MyVariable = MyVariable + 1
    7.   MsgBox MyVariable  'should show 11
    8.  
    9.   MyVariable = MyVaraible - 2
    10.   MsgBox MyVariable  'should show 9
    Instead of showing us "10", "11", "9", the messages actually show us "10", "11", "-2"!

    The reason for this is that I mis-spelt the variable name (MyVariable = MyVaraible - 2), so VB being 'kind' creates a new variable (which has a default value of 0), and uses it in the calculation.

    In the code above it is fairly easy to spot the mistake, but the more code you have the harder it gets to find mistakes like this - generally all you know is that the code is not working properly, but you can't tell why.

    Instead of spending lots of time trying to work it out, simply having Option Explicit at the top of the code file will tell you what (and where) the problem is, so all you need to do is correct the variable name (or declare the variable, if it was meant to be a different one!).


    Can I have it added to my code automatically?
    Yes you can, but only to new files that you create - you will need to add it yourself to existing files.

    To have it added to all new files you create, simply select "Tools" -> "Options", and tick the "Require Variable Declaration" box.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Classic VB - What is Option Explicit, and why should I use it?

    The reason for this is that I mis-spelt the variable name (MyVariable = MyVaraible - 2), so VB being 'kind' creates a new variable (which has a default value of 0), and uses it in the calculation
    May I clarify few things please...
    When variable declaration isn't required then for each new name space VB will allocate memory as it would for Variant type - this could actually mean major performance issue.
    Initially new variable is Empty due to being variant. However since VB evaluates each expression before it executes one it will assing ZERO to a variable if it is part of mathematical equation.

    The bottom line: regardless of whether or not Option Explicit is used (it should be in my opinion) it's always a great idea to explicitely declare the variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.