VKS-LEARNING HUB

Home » Class-XI » MMWT-XI » VB Script » VB Script Basics

VB Script Basics

What Are VBScript Data Types?

VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it’s used. Because Variant is the only data type in VBScript, it’s also the data type returned by all functions in VBScript.

 Variant can contain either numeric or string information. A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you’re working with data that looks like numbers, VBScript assumes that it is numbers and does the thing that is most appropriate for numbers. Similarly, if you’re working with data that can only be string data, VBScript treats it as string data. Of course, you can always make numbers behave as strings by enclosing them in quotation marks (” “).

Variant Subtypes
Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, you can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time. Of course, you can also have a rich variety of numeric information ranging in size from Boolean values to huge floating-point numbers. These different categories of information that can be contained in a Variant are called subtypes. Most of the time, you can just put the kind of data you want in a Variant, and the Variant behaves in a way that is most appropriate for the data it contains.

The following table shows the subtypes of data that a Variant can contain.

Subtype Description
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-length string (“”) for string variables.
Null Variant intentionally contains no valid data.
Boolean Contains either True or False.
Byte Contains integer in the range 0 to 255.
Integer Contains integer in the range -32,768 to 32,767.
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long Contains integer in the range -2,147,483,648 to 2,147,483,647.
Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999.
String Contains a variable-length string that can be up to approximately 2 billion characters in length.
Object Contains an object.
Error Contains an error number.

VARIABLE

Variable is name of Memory space to hold value

Variable can be declare in two ways: Implicit & Explicit declaration

Implicit Declaration : We can define variable anywhere in the program by assigning a value to a variable.

Example 1:    A=10 means we have declared a variable A which holds the value 10 which is integer

Example 2:    A=”WEB-TECH” means we have declared a variable A which holds the value “WEB-TECH” which is String

Explicit Declaration :

We can define variable anywhere in the program by using a keyword DIM. In Explicit Declaration first the variable is declare using DIM statement and then assigned any value. We cannot declare and assigned a value at the same time i.e

Dim A=10 is wrong declaration

Correct Way :

Dim A

A=10

Option Explicit Statement:

Option Explicit is used in the beginning of program before any declaration of variable. It forces the explicit declaration of variable by using Dim. Any variable declare without using Dim statement is not allowed and give error.

Option Explicit

<Script Language =”VBScript”>

Dim A,B

A=10 correct

B=20 correct

C=A+B  incorrect as variable C is not declare with dim so it is not allowed and give error

Correct way

Dim C

C=A+B

Naming Restrictions for Variables

Variable names follow the standard rules for naming anything in VBScript. A variable name:

  • 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.

Variable Scope

Variables in VB Script have three levels of scope: page level scope, script level scope, and procedure level scope.

The script level variables are declared right at the beginning of the script and are available to all procedures within that page’s script  They are Global Variable .

Variables with a procedure level scope are declared in the procedure they are used in and are visible only within the context of that procedure. They are called Local Variable

Outside of procedures you declare a variable as either Private or Public.

The variable that is declared as Private is visible to only the procedures within the script it is declared in.

The variable that is declared as Public is visible to all of the scripts on that HTML document.

Table : Variable Scoping in VBScript

KeyWord                            
Placement                                   
Description of scope                                           
Dim
In Procedure
Limited to the procedure
Dim
Not in Procedure
Visible throughout script
Private
Not in Procedure
Visible throughout script
Public
Not in Procedure
Visible throughout all scripts

Scalar Variables and Array Variables

A variable containing a single value is a scalar variable.

A variable containing a series of values, is called an array variable.

Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.

Example:

Dim A(3)

Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.

We assign data to each of the elements of the array using an index into the array.

Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:

A(0) = 256

A(1) = 324

A(2) = 100

A(3) = 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want.

For example:

SomeVariable = A(4)

HOW to Declare fixed length Array  in  VBSCRIPT

<Script Language =”VBScript”>

Dim name(10)

</script>

HOW to Assign Values to index of Array  in  VBSCRIPT

<Script Language =”VBScript”>

Dim name(10)

name(0)=”neeraj”

name(1)=”dheeraj”

name(2)= “madhu”

..

..

..

..

Name(9)= “Shweta”

</script>

The index of an array always start from 0 and ends with N-1

 To get an item from Array just refer to index number like

 Myname=name(2) will store “madhu” to variable Myname

Arrays aren’t limited to a single dimension. We can have as many as 60 dimensions, although most people can’t comprehend more than three or four dimensions.

In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5, 10)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.

 Dynamic Arrays

We can also declare an array whose size changes during the time our script is running. This is called a dynamic array.

The array is initially declared within a procedure using either the Dim statement or using the ReDim statement.

However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.

For example:

Dim MyArray()

ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.

Resizing a dynamic array without Preserve.

You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array.

Dim arrMyArray()

ReDim arrMyArray(25)
Redim arrmyArray2(30)

In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30,

all the values stored in arrMyArray(25) will be empted when we use Redim to declare the size 30 by Redim arrmyArray2(30) .

Resizing a dynamic array with Preserve

ReDim MyArray(25)

ReDim Preserve MyArray(30)

The Preserve keyword is very important when using ReDim. Suppose, for  example, that you create a dynamic array, specifying its storage space by using ReDim, fill it with data, and then later decide to make it larger so you can fill it with more information without losing your original data.
In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

There is no limit to the number of times we can resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.


Leave a comment