Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

VBScript Tutorial
VBScript Introduction
VBScript How to Use
VBScript Variables
VBScript Procedures
VBScript Functions
VBScript Conditions
VBScript Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


VBScript Variables
Previous Next

Working with Variables

When a variable declared it reserved a space in computer memory that can be use for storaging data during the execution of scripts. You can use variables to:

  • Store input from the user via web page
  • Save data returned from procedures and functions
  • Hold results from calculations




Declaring Variables

Dim statement, the Public statement, and the Private statement are used to declare variables in script, explicitly. For example:


Dim Fahrenheit


You also declare multiple variables by separating each variable name with a comma. For example:

Dim Left, Right,Top, Bottom.


You can also declare a implicit variable by simply using variable name inside script. But it is not a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.




Naming limitations
  • Must begin with a letter 


  • Cannot contain a period (.)


  • Cannot exceed 255 characters


  • They must be unique within the same scope.



Variants and Subtypes

VBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The following table describes the subtypes supported by VBScript.


Subtype Description of Uses for Each Subtype
Byte Integer numbers between 0 to 255
Boolean True and False
Currency Monetary values
Date Date and time
Double Extremely large numbers with decimal points
Empty The value that a variant holds before being used
Error An error number
Integer Large integers between -32,768 and 32,767
Long Extremely large integers (-2,147,483,648 and 2,147,483,647)
Object Objects
Null No valid data
Single Large numbers with decimal points
String Character strings



Assigning Values

To assign a value to the variable

Variable_name = value

The following examples demonstrate how to assigning values to variables:

Name = "Larry Roof"

HoursWorked = 50

Overtime = True



Scope and Lifetime of Variables

Lifetime means How long a variable exists

When you declare a variable inside a procedure, the variable can only be accessed within that procedure. When the control of program reach outside the procedure, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.

All the procedures can access the variable if varibles declared in outside a procedure. The lifetime of these variables starts when they are declared on page load time and ends when the page is closed.

The following example descibe below demonstrates both script-level and procedure-level variables.

<SCRIPT>

Dim counter

Sub cmdButton_onClick

Dim temp

End Sub

</SCRIPT>


The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure.



Constants

VBScript does not provide support for constants, such as other programming languages. You can work by assigning values to variables that you have defined as shown given example. Here, TAX_RATE is our constant.


<SCRIPT>

Dim TAX_RATE

TAX_RATE = .06

Function CalculateTaxes

CalculateTaxes = CostOfGoods * TAX_RATE

End Function

</SCRIPT>




Scalar Variables and Array Variables

A variable containing a single value is a scalar variable. To assign more than one related value to a single variable you can create a variable that can contain a series of values. This is called an array variable. Scalar variables and Array variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 10 elements is declared:


Dim A(9)

Although the number shown in the parentheses is 9, all arrays in VBScript are zero-based, so this array actually contains 10 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.
You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 9, data can be assigned to the elements of an array as follows:
 


A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(9) = 55


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

 . . .
SomeVariable = A(8)
. . .


Arrays aren't limited to a single dimension. You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. Multiple dimensions are declared by separating an array's size numbers in the parentheses with commas. In the following example, the user_Table variable is a two-dimensional array consisting of 5 rows and 10 columns:


Dim user_Table(4, 9)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.
You can also declare an array whose size changes during the time your 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 declare a dynamic array, use ReDim to determine the number of dimensions and the size of each dmension. In the following example, ReDim sets the initial size of the dynamic array to 20. A subsequent ReDim statement resizes the array to 25, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.


ReDim MyArray(20)
. . .
ReDim Preserve MyArray(25)

No limit how many number of times you can resize a dynamic array, but you should know that if you make an array smaller than it's current size then you lose the data from that particular variables



Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: independent variables, types of variables, dependent variables, php session variables, windows environment variables, controlled variables, tube expanding procedure variables essential, linear equations in two variables, confounding variables, demographic variables


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.