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

There are 5 web pages.write a script to click the button on 4th web page.

3207


what is the object hyrarchy in QTP for a web based application

2109


why variable name should not exceed 255 characters?

2007


I need to get some data from data base and store this (retrieved) data in a excel sheet using VB script in QTP9.0 I have created connection for data base I have created as excels sheet by using Set XL=CreateObject("Excel.Application") XLworksheet.cells(1,1).value= rs.fields.item("<>") I have taken a for loop and changed the cells values (1 as i and another 1 as j) But still I am not able to get Plz kindly tell me know this . It is very urgent

1994


How can you fetch the value of a cookie?

784


Explain about adodb.stream class?

825


i am having some basic knowledge in c and VB 6.0. i qtp i want to know how to write the codings. by recording it is generating lot of codings. for example i want to test the text box. senerio is text box should not accept alphabets if it accept alphabets we should return fail status if it get number we should return pass status. how to write coding in vb script please help me

2039


Why is the use of exit do or exit for statements within loops discouraged?

783


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

1809


Explain sga memory structures?

802


Why is it recommended to close the database connection every time after the work is completed?

718


What are subprocedures in vbscript?

879


What are the different types of operators and their order of precedence?

804


Write a vbscript procedure that converts feet to inches. Hint: there are 12 inches in a foot?

818


How to Enter Values on the Command promt using VB script

1759