VBScript Part 2 - Variables

1 Star2 Stars3 Stars4 Stars5 Stars (7 votes
Loading ... Loading ...

Your Ad Here

What is variable?
Variable is a named container used to store values

Declaring a variable
There are three possible ways to declare a variable

Dim <variablename>
Public </variablename><variablename>
Private </variablename><variablename></variablename>

Ex –

Dim x, y, z
Dim a
Dim b, c

Variable naming rules
Variable names should following the below rules

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

Though these are the naming conventions specified by Microsoft. But we will be breaking the last 2 rules later in this article
Assigning values to variables
Variables can be assigned values why using the “=” to operator. Below code shows how different types of values can be assigned to a variable

Dim myVar
myVar = "Assigning a string value"
'Assigning a integer value
myVar = 2
'Assigning a double value
myVar = 3.3

Data Type of variables
All variables declared in VBScript are of Data type VARIANT. A VARIANT data type can store any types of value listed below

  • Boolean
  • Byte
  • Date (Time)
  • Double
  • Error
  • Integer
  • Long
  • Object
  • Single
  • String

All actual type of the data stored in a VARIANT type is known as the sub-type of the variable.

Declaring constant variable
Constants can be declared using below syntax

Const  <constname> = <constvalue></constvalue></constname>

Ex –

Const APP_ NAME = "Testing VBScript"
Const APP_VERSION = "1.01"

It is advised to use all caps variable name when declaring a constant.

Variable Naming conventions
Since variables can store various types of values it is always a good practice to name them with a consistent convention

Data Type

Prefix 1

Prefix 2

Prefix 3

Boolean

b

b_

bln_

Byte

by

by_

byt_

Date (Time)

dt

dt_

dt_

Double

Dbl

dbl_

dbl_

Error

err

err_

err_

Integer

i

i_

int_

Long

l

l_

lng_

Object

o

o_

obj_

Single

sng

sng_

sng_

String

s

s_

str_

Above table shows few prefixes that can be used during scripting. You should choose the ones you like and stick to them. I personally prefer the Prefix 2 column. Below is the list of other Prefixes that might be required

  • Function – fn
  • Sub – sub
  • Global variable – g
  • Class member – m
  • Class – cls

We would be discussing all above mentioned topics in the upcoming articles

Forcing variable declaration
VBScript allows a very poor programming practice which is not to declare the variable being used
Ex –

str_Message = "Tarun Lalwani"
Msgbox str_Message
Msgbox for a undefined variable

Msgbox for a undefined variable

Above code will work even when we haven’t declared the ” str_Message” variable using a Dim/Public/Private statement. VBscript when it looks at a variable name which is not yet declared it declares with an Empty value. In programming a typo when using the variable name is always a possibility. Ex –

str_Message = "Tarun Lalwani"
Msgbox str_Messge
Msgbox for undefined and unused variables

Msgbox for undefined and unused variables

The value above is empty message because we haven’t declared str_Messge variable and when used VBScript declares it with an empty value. Though it is pretty easy to spot the issue in above code but when the code size is huge, these issues become very difficult to debug as wrong variable name used at once place might cause an issue at different location of code. To avoid such a situation VBScript provides an “Option Explicit” statement which can be used at the top of the code as shown below

Option Explicit
str_Message = "Tarun Lalwani"
Msgbox str_Messge

Now above code would throw an error

Undefined variable error when Option explicit is used

Undefined variable error when Option explicit is used

Breaking the 2 rules of naming restriction
Let us reiterate the last 2 rules for variable naming in VBScript

  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.

Thought this is pretty much true the normal way
All below declaration will give syntax errors

Dim 1A
Dim _AB
Dim A B
Dim A.B

Though the error might be different for above declarations but the source of the error is the same, bnot following the naming convention. Now VBScript provides a very less talked about square bracket “[]” declaration

Dim [9X]
Dim [_varName]
Dim [My Name]
Dim [this.A]
[My Name] = "Tarun Lalwani"
[this.A] = [My Name]
MsgBox [this.A]
Msgbox for a undefined variable

Breaking the variable declaration rules

Have questions related to this article? Want to request a new article? Use our forums to post your questions


Viewed 4,027 times
Free Advertising

Related posts:


    Support KnowledgeInbox (Why donate?)




    Your Email Address:



  1. Most Viewed


  2. Earn $$ with WidgetBucks!
  3. Sitemap

    open all | close all
  4. Most Downloaded

    • Screen Capture and Comparion API - Version 1.1.1 (425.5 KiB, 5,586 hits)
      You need to be a registered user to download this file.
    • QTP Clean uninstaller (26.4 KiB, 5,005 hits)
      You need to be a registered user to download this file.
    • VB to QTP API Converter (37.2 KiB, 4,393 hits)
      You need to be a registered user to download this file.
    • IE Helper setup (3.5 MiB, 4,132 hits)
      You need to be a registered user to download this file.
    • Dynamic Action Call Demo - Part 1 (40 KiB, 3,643 hits)
      You need to be a registered user to download this file.
  5. Most Rated

  6. Translator

    English flagItalian flagKorean flagChinese (Simplified) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flag
    Croat flagDanish flagFinnish flagHindi flagPolish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flag
    Slovak flagSlovenian flagUkrainian flagVietnamese flag           
    By N2H
  7. Last referers

    Visitors Online



Your Ad Here