Monday, April 12, 2010

VB Script - Variable - Explicit Declaration

Previous12345678910Next

Explicit Declaration


Explicit Declaration is used to avoid the unnecessary confusion created while typing and also to avoid loading the memory with variables because of errors we can use Explicit Declaration. In order to use explicit declaration type we need to declare the variables before the script begins or it should be in the starting of the script.


We will use the same example used for Implicit Declaration but will change the script. In order to create Explicit Declaration in the beginning of the code we need to add one more line of code as ‘Option Explicit”. Take a look at the code for Explicit Declaration

Option Explicit
Dim Int1, Int2, Prod
Int1 = 28
Int2 = 30
Prod = Int1 * Int2
Msgbox Prod

Now lets debug the code. If by mistake instead of “Prod” you type “Prd” in Line 6 as Msgbox Prd then what will happen. Since “Prd” is not declared when you run the script it executes the first five line and when it executes the sixth line it throws error and till it is not rectified the resultant output is not displayed. It will throw error as “Variable is undefined: ‘Prd’.

This is the advantage of using Explicit Declaration.

Previous12345678910Next

No comments:

Post a Comment