Results 1 to 5 of 5

Thread: Option Explicit

  1. #1

    Thread Starter
    Hyperactive Member PITBULLCJR's Avatar
    Join Date
    Nov 1999
    Location
    New York
    Posts
    408

    Post

    What exactly does this do. I have found in some programs I needed it and in others if I do use it it crashes. Like for instance I can't get this code to work if I put in option Explicit

    Sub Timeout(duration)
    starttime = Timer
    Do While Timer - starttime < duration: DoEvents
    Loop
    End Sub

    Private Sub Command1_Click()
    Timeout (10)
    Label1.Caption = "hello"
    End Sub


    ------------------
    Sincerely,
    Chris
    :-) ;-)
    Email pitbullcr7@aol.com

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Option Explicit makes sure that any Vairables you use are Pre-Defined, otherwise it'll give you an Error Message telling you it isn't.

    It's useful for making sure you keep your Programs Tidy, it can also help if you make a Typo with a Vairable name.

    If you don't use Option Explicit VB doesn't care if you create variables on the fly, i.e

    A = 1

    Instead of:

    Dim A As Integer
    A = 1


    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    ajyoung@pressenter.com


  3. #3
    New Member
    Join Date
    Jul 1999
    Posts
    5

    Post

    PITBULLCR,

    Your program works.

    Option Explicit forces you to declare all variables in a module.

    If you don't use option explicit, all undeclared variables are variant types.

    Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.

    Joe


  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    I would suggest you change your project properties so that you always use Option Expicit. Without it you might spend a lot of time figuring out why the following code never ends.
    Code:
        Dim MyMispelledValue As Integer
        
        While MyMispelledValue < 100
            ' do something
            MyMispelledValue = MyMisspelledValue + 1
        Wend
    ------------------
    Marty
    Can you buy an entire chess set in a pawn shop?

  5. #5
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Post

    also if you don't use option explicit and all you variables are variant your program could compile significantly larger than if you declare strings, longs,.....

    ------------------
    SomeTimes Coffee Just Isn't Enough.
    Zan Magi

Posting Permissions

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



Click Here to Expand Forum to Full Width