QTP v10 Learn Online - On Sale Today

11
Mar 10

Why Declare a Variable in VBScript?

VBScript is a very flexible scripting language that provides full programming capabilities for various implementations.  VBScript is the programming language used by QuickTest Pro, so when you are in the Expert View doing some advanced programming you will regularly create variables and use them to hold various data values.  All variables in VBScript are of one fundamental data type – Variant.  Since you can declare a variable implicitly by simply using its name in your script at any point in time, then why bother to take the time to explicitly declare you variables before using them?  The main reason (really the only one) is to avoid typos!
Let’s look at a code snippet; this code block will take a street address that was retrieved from one part of the application and compare it with the address originally entered into the system.  However when the address was retrieved it came back with extra spaces so when comparing the addresses within code using a comparison statement it would fail, so this code block was written to strip off any extra spaces.  Realize there are many ways of approaching this problem; this is just one approach to emphasize the topic at hand:


extraSpace="123 Testing Lane "
address="123 Testing Lane"addrLength=len(extraSpace)
counter=1
pos=right(addrLength,counter)while pos = " "
counter=counter+1
pos=right(addrLength,counter)
wendcounter=counter-1
cnt=addrLength-counternewval=left(addrLength,cnt)
if newval = address then
MsgBox ("They are the same.")
else
MsgBox ("They are different.")
end if

Notice in the code block there are many variables used.  The concern about not declaring your variables up front is that when you access a variable later on in the code (like the variable “addrLength”), if you accidentally typed the variable name incorrectly you would end up with an incorrect result.  What adds to the problem is that it is difficult to figure out why the result is wrong because you might not catch the problem and waste a lot of time trying to figure out why.  So if I ended up typing this line within the code block instead:


cnt=addrLenght-counter     

I would end up with the wrong results all because of a typo.  The problem with implicit declarations is that VBScript says “hey this person wants to create a new variable” and just creates a new one called “addrLenght” and assigns the value of Null to it.  So now your count will be totally off for this formula.  But if you had declared your variables at the beginning of the code using the “Dim” statement, then you could avoid this issue.  As part of this approach you would want to also add the statement “Option Explicit” just before the declaration like this:


Option Explicit
Dim extraSpace,address,addrLength,counter,pos,newval,cnt     

The Option Explicit requires that all variables be defined in the code prior to using them.  So although this approach doesn’t come out and say “hey you have a typo”, what it does say is that “addrLenght” was not declared and this should help you to identify that there is a typo because you would say “I did declare it, it’s right there in the Dim statement”.  When you then look at the variable name in the formula you would then notice the typo.  Without this, the formula just produces the wrong results with no error message or anything to help you out.

Defining variables up front is a good habit to get into because the majority of programming languages require you to define all variables at the beginning of your program before using them.  So in being consistent with most programming standards is a great practice to get into.

Note that in VBScript, Constants and Arrays must be declared before using them.  This topic only applies to scalar variables.

Variable Name Restrictions

  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.
  • Must not exceed 255 characters.
  • Must be unique in the scope in which it is declared.

by Shawn | About the author: Shawn LoPorto brings 23 years of diverse technology and business experience with strong technical skills, leadership, problem-solving, planning, team development and project management skills. Shawn is a Vice President of QA & CM. See the Profile page on this website for more information.

Related Posts


Comments


Posted on Thursday, March 11th, 2010 at 11:08 am and is filed under ASI General, Programming, QuickTest Pro, Sample Code, Tools. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.
1 Comment so far

  1. 1 Eddi on July 11, 2011

    Smack-dab what I was looking for ty!

Name (required)

Email (required)

Website

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Share your wisdom