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

Explain the asc function?

716


What is the output of a + b in vbscript if a = 5 and b = 10?

2809


What is the purpose of regexp object in vbscript?

804


Mention the rules for using option explicit statement?

729


What purpose does ‘on error resume next’ serves?

825


In html file what is an ideal position to include vbscript?

1105


What is the purpose of on error resume next statement?

792


How many types of operators are available in the vbscript language?

771


How to create a cookie using vbscript?

881


How will you get the largest subscript of an array in vbscript?

899


Hellow friends, I am learning QTP,but here problem is VB script. please guide me how to learn VB script w.r.t QTP and if you know any books tell me or if you have any materials or any use full material or any else w.r.t QTP please post me p.p.sekhar

2265


Explain about adodb.stream class?

846


Which loop is used in case of arrays in the vbscript language?

741


How to scroll down a page while recording in qtp. suggest me any method apart from low level recording.

2833


What is dictionary object in vbscript? Explain?

906