Exforsys.com
 

Sponsored Links

 

VBScript Tutorials

 
Home Tutorials VBScript
 

VB Script - Working with Variables

 

VB Script - Working with Variables

Page 1 of 2

VB Script - Working with Variables

In this tutorial you will learn about VB Script - Wroking with variables, Introduction, Description, Naming Conventions, Variants and its sub types, How to Assign values to a variable ? What is the scope of a variable ? Constants, Arrays and Conclusion.



Introduction

A variable is a named location in your computer memory which can be used for data storage during the working of your vb scripts or any other language.Variables plays a vital role to store values . Variables can be defined further into two categories.


One is local variable and second are global variables. Local variables values will be only accessible in the part of the function in which that variable has been defined whereas global variable will be accessible in the whole program.


Description

Variables store input from the user that is being collected via your web page. Please see the below example for a variable.


Sub cmdClickMe_onClick
Name=InputBox(“Please Enter your Name”)
MsgBox “ The Name you entered was “ & Name
End Sub


Now, in the above example the second line shows a variable with the name of “Name”. Here one Input box will be displayed to the user with a message Please enter your name and what ever entered by the user will be stored in a variable called Name and later on displayed to the user with the help of MsbBox function.

There is something left in the variables i.e How to declare a variable. There are two types in which variable is declared one is explicitly and one is implicitly. Well the preferred method is explicitly with the help of dim statement.

For ex. Dim age

Here we have declared variable with the name of age . In this way you can also declare multiple variable in a single line only.

For ex. Dim name,age,status,birthdate

The above given examples are of explicit declaration of the variables. For implicit declaration you can declare and use the variable at any time within your code.But the main disadvantage of implicit declaration is that it is very hard to debug the code when the error occurs. So in my opinion always prefer the variables to declare explicitly. There are some naming conventions attached when you declare a variable.


Naming Conventions

Whenever you want to declare a variable always follow below naming conventions.


  • The variables always begin with an alphabet instead of any number.
  • They should not contain spaces.
  • They must be unique in their names.
  • If you want to define a long name than always concatenate the names with and underscore.

Ex. Fname_Lname


Always start your variable name with capital alphabet letter and when the second word starts in the variable make it capital too.
They must not be larger than 255 characters in length.


Variants and its sub types

VbScript deals with a single datatype called variant. Datatype means the kind of the data a variable will store.It can be a character data,string data,number data , byte data and a Boolean data. But you don’t have to worry in Vb Script as it only has a datatype of variant to store any type of data.


Sub Type


Description


Boolean


True or False


Integer


Integers between –32768 and 32767


Long


Long data between –2147483648 and 2147483647


String


Character Strings


Single


Large Number with decimal points


Null


No valid data


Currency


Monetary values


Byte


Integers from 0 and 255


Date


Date and time


Double


Large numbers with decimal points



How to Assign values to a variable ?

Simple you have to declare a variable name and assign any value.


For ex. Name = “Chandra”
Status=False
Age=30


Now all the above variables has been assigned values.This is a simple way to declare and assign related values to a variable.


What is the scope of a variable ?

The scope of a variable defines whether a variable will be accessible in the whole function or will be accessed only to its local instance.I have defined earlier in the tutorials that they can also be deemed as a local variables or can be deemed as a global variables.


For ex.


< script >
Dim name
Sub cmdclickme_OnClick
Dim age
End Sub
< / script >


It is clear from the above example about the scope of the variable that the variable name will be available to the whole script as it is declared outside sub procedure so enhance his behaviour to the global as compared to the variable name age which is defined inside the sub procedure hence making his behaviour local and will be only accessed in this sub procedure only.


Constants

Vb Scripts does not support constants. Constants are the values which are not changed throughout the program as you might have read in other programming languages.


Arrays

If you have done any programming language you might have heard about the Arrays and VbScript positively supports arrays. You can declare any array with the help of dim statement.


For ex. Dim country(20)

The above array has been created with the 20 elements, it means you can easily store 20 elements and will be referred by a single name. These type of arrays are sometimes called fixed arrays.Arrays index always start from zero. As given in the above example we have declared an array of 20 elements but when you put the values or retrieve the values from the array you have to start from the index 0 to index 19.To store values in an array just follow the simple strategy.

country(0)=”India”
country(1)=”US”
country(2)=”Canada”

Arrays too have a multi dimensions . To declare a multi dimensional array example is given below.Suppose you want to store a student name as well his/her age.To store the values in an array you have to reference both dimensions.

Dim nameage(30,0)=”Rahul”
Dim nameage(30,1)=”Mehta”
Dim nameage(30,2)=”Chandra”

As in the above example we know how many values will be stored in an array.But sometimes we don’t know how many values will be stored in an array than there arises a need for dynamic arrays.To declare a dynamic array see the below example.

Ex. Dim users()

In this way we can declare a dynamic array without initializing its size.But to change the size you can use the below given code.

Ex. Redim users(100)

In this way you can assign the size of the array. Now whenever you want to increase the size of the array again you must preserve the previous values you have stored in an array.To preserve the previous values use.

Redim Preserve users(100)



Next Page: VB Script - Working with Variables - Page 2


Read Next: Working with Objects in VB Script



 

 

Comments


sudhap said:

  hi
in the above program what is document variable and from where it comes
i tried this program
but it is not working
waiting for reply
veena
March 8, 2007, 8:15 am

Rick Shultz said:

  Good basic tutorial. Thanks.
June 24, 2008, 5:05 am

ghaazi said:

  hi

I have a problem i want to convert a value into a variable how can i do that? here is my sample code.

'dim onjName11
'dim onjName12
'dim onjName21
'dim onjName22

onjName11=22222222
onjName12=33333333
onjName21=44444444
onjName22=55555555

for i = 1 to 2
vale="onjName" & i &"1"

WScript.Echo vale
next
June 10, 2009, 2:36 pm

Mukesh said:

  Dear Ghaazi,
Its never possible to use for loop for values within a text

Regards,
Mukesh
June 25, 2009, 4:39 am

Scott said:

  VBScript does support contants, as of version 5: http://msdn.microsoft.com/en-us/library/16twy8ed(VS.80).aspx
July 22, 2009, 4:09 pm

krishna said:

  is it possible in VB Script to define Multiple Variables in a line.

Example
Dim x,y,z
x=5,y=6,z=8
January 29, 2010, 6:47 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape