Why do we use Option Explicit?

Answer Posted / jim c

Option Explicit forces the programmer to declare all
variables, rather than letting them be implicitly declared
upon their first use.
This enforces good programming practices and avoids
ambiguity of variable scope.
Here are some simple code examples:
'*************************************
i = 1
Call mySub
WScript.Echo i

Sub mySub
i = 10
End Sub
'*************************************
In this first example, because the programmer is relying on
implicit variable declaration, the subroutine is modifying
global variable i, and the printed result is 10. Now, modify
the code so that the variables are declared:
'*************************************
dim i
i = 1
Call mySub
WScript.Echo i

Sub mySub
dim i
i = 10
End Sub
'*************************************
Now, we actually have two different variables named i, one
with global scope, and one with local scope. The printed
result is now 1.
Since a lot of programmers use i in For..Next loops, you can
see how this could result in unpredictable results without
explicit variable declaration

Is This Answer Correct ?    8 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Mention when to use function procedures and what are its characteristics?

624


When to use function procedures and what are its characteristics?

547


Explain about .wsf files?

616


where can i learn VB scripint ?

1591


what do you mean .ota mobile format

1935






Which command is used for writing text on a page?

572


wht must be the interview question on corinthian information technology solutions incorporated.

2181


How are values assigned to the variables in the vbscript language?

567


What is select case statement?

592


Please let me Know regarding any material regarding VB Scripting which should be easily understandable for Beginners.

1530


How will you convert a string to upper case string using vbscript?

548


How can you fetch the value of a cookie?

543


Is VB Script Case sensitive or Case insensitive?

631


Explain about the functionality of vb script?

565


how to acces the remote mechine using vb cript(QTP)

1598