Results 1 to 7 of 7

Thread: Option Explicit?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Question

    What does Option Explicit do? <-- That's my question! I've made so many big usefull programs with API and so much more but I never knew why I had to put Option Explicit at the top of every single one of them! Thanx in advance for your help!
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  2. #2
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Adding Option Explicit to your app will raise an error if you try to use a variable that hasn't been declared.

    For example...

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
      s = "HELLO WORLD" ' causes error to be raised because s is not declared
    
    End Sub
    Code:
    Private Sub Form_Load()
    
      s = "HELLO WORLD" ' works fine because Option Explicit is not used
    
    End Sub

    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879
    Does it declare the variable if it's not declared! FOr Ex:
    Code:
    Option Explicit
    
    Function OpenQ(OpL As String)
    s = "hello" '<-- would that generate a variable called S
    End IF
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  4. #4
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Nope that code would cause an error because you didn't declare it, if you want to use variables in that way then you need to get rid of the Option Explicit statement.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If you don't use Option Explicit, it implicitly declares variant variables for you.
    I would recommend to stick to the Option Explicit statement, because it warns you for unwanted variable declarations. A typo could be difficult to find, if no error is generated, but another variable is declared instead.

    If you get tired of typing the Option Explicit statement, go to Tools --> Options and check the Require Variable Declaration checkbox. All new code modules will have the Option Explicit statement added automatically.

  6. #6
    Member cramtheman's Avatar
    Join Date
    Dec 2000
    Posts
    43

    Red face

    I know theres someway you can make it so that option explicit is automatically there for very form you make, how do you do this?

    Thanks
    cram
    [email protected]
    _ _ _ _ _ _ _ _ _ _ _ _ _ _
    Things to Ponder

    |Man who stands on toilet,
    Is high On pot|
    |Baseball Wrong,
    Man with four balls cannot walk|

  7. #7
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Look at Frans C's post directly above yours.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

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