My Blog List

Thursday, May 3, 2012

Option Explicit


Option Explicit: Why we need this statement while writing VBScript?

-- By default, VBScript will allow use of variables without declaration which leads to lot of mis-spell, type and wrong variable name errors.
-- This will force the VBScript Engine not to accept the variables without declaration before its first use.
-- Normally, Used to avoid typo errors, giving wrong variable names, redefining variables by mistake, etc.

Consider the below script:
  1. Dim var1
  2. var1 = 10
  3. 'This line will not throw any error like "Variable undefined"
  4. var2 = 20
  5. 'This line will not throw any error like "Variable redefined"
  6. Dim var1
After inserting Option Explicit Statement, it will throw error.
  1. Option Explicit
  2. Dim var1
  3. var1 = 10
  4. var2 = 20     'This line will throw error "Variable undefined"
Also,
  1. Option Explicit
  2. Dim var1
  3. var1 = 10
  4. Dim var1      'This line will throw error "Variable redefined"

The same functionality is available in JavaScript by typing "use strict" like below;
  1. "use strict";
  2. var var1 = 10;
  3. alert(var1);

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete