Results 1 to 5 of 5

Thread: Option Explicit Statement from Visual Basic 6.0

  1. #1
    Join Date
    Jan 2009
    Posts
    69

    Option Explicit Statement from Visual Basic 6.0

    What is the reason that many of the programers uses the Option Explicit statement into the Visual Basic 6.0? Can anyone has the details about it? Please help me about this? I am waiting for your reply.

  2. #2
    Join Date
    May 2008
    Posts
    2,015

    Re: Option Explicit Statement from Visual Basic 6.0

    HI, Basically Option Explicit Statement Forces explicit declaration of all variables in a file. It has following syntax:
    Code:
    Option Explicit { On | Off }
    On - It is Optional. It enables Option Explicit checking. Bydefault it is on.
    Off - It is also Optional. It is used to disable Option Explicit checking.

    To set Option Explicit in the integrated development environment (IDE) follow the following steps:
    • On the Tools menu, choose Options.
    • Open the Projects and Solutions node.
    • Choose VB Defaults.
    • Modify the Option Explicit setting.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,010

    Re: Option Explicit Statement from Visual Basic 6.0

    Hi, if you are using Option Explicit statement in your source code then it must appear in a file before any other source code statements. When Option Explicit appears in a file, you must explicitly declare all variables using the Dim or ReDim statements. If you attempt to use an undeclared variable name, an error occurs at compile time. So basically Option Explicit is used 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. The compiler bydefault sets Option Explicit On, if you don't specify Option Explicit in your code. But, it is a good programming practice to use the option explicit in your code. So, try to use it into your code.

  4. #4
    Join Date
    May 2008
    Posts
    2,302

    Re: Option Explicit Statement from Visual Basic 6.0

    Hi, use of Option Explicit in you code forces explicit declaration of all variables in a file. It has following syntax:
    Code:
    Option Explicit { On | Off }
    You can get some more clear information with the use of following example. c

    Code:
    ' Force explicit variable declaration.
    Option Explicit On
    
    Dim variable As Integer
    variable = 588
    ' The following assignment produces a COMPILER ERROR because
    ' the variable is not declared and Option Explicit is On.
    number = 10 ' will causes ERROR

  5. #5
    Join Date
    Oct 2005
    Posts
    2,394

    Re: Option Explicit Statement from Visual Basic 6.0

    Hi, you will see the Option Explicit statement at the top of VB code.This statement forces you to declare your variables before you use them. Just simple. By placing Option Explicit at the top of every code module before any procedures you will become safe regarding not the use of any variable without declaration. For example.
    Code:
     Private Sub Command1_Click()
        Dim one As String
        oone = "Hello"  
        MsgBox one
        End Sub
    This code will give you a blank message box. Due to the wrong use of the variable. Now if you used the Option Explicit at the top of the code, you get a "Variable not defined" error. This is a simple fix for what could be a complex problem. It also increase your speed of code execution.

Similar Threads

  1. How to write a SQL statement in Visual Basic?
    By - Empty Shell - in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 02:54 AM
  2. Replies: 3
    Last Post: 30-10-2009, 06:27 PM
  3. what are Visual Basic IDE?
    By Naresh Modi in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 08:49 AM
  4. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 12:32 PM
  5. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 05:56 PM

Posting Permissions

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