Asp.net Classic

Variables

Asp.net Classic / Variables

Variables

Variables in ASP

 

Variables are used as containers to store information that can be reused and modified while a program is running.

More Examples with Variables

 

Declaring a Variable

This example shows how to create a variable, assign a value to it, and display that value within text.

Creating an Array

Arrays allow you to store multiple related values in a single variable. For example, an array can be used to store a list of names.

Looping Through HTML Headings

This example demonstrates how to loop through all six HTML heading tags using a loop.

Time-Based Greeting (VBScript)

This example displays different messages depending on the current server time using VBScript.

Time-Based Greeting (JavaScript)

This example performs the same task as above but uses JavaScript syntax instead.

Creating and Modifying a Variable

This shows how to declare a variable, assign a value, and later update that value.

Inserting a Variable into Text

This example explains how to display a variable’s value inside text output.

Understanding Variables (Algebra Example)

 

From basic algebra, we know:

x = 5

y = 6

z = x + y

Here, letters like x, y, and z are used to store values or expressions. These letters are known as variables.

Similarly, in programming, variables are used to store values or expressions that can be reused in calculations and logic.

VBScript Variables

 

In VBScript, variables serve the same purpose as in algebra: they hold values or expressions.

A variable name can be short (like x) or descriptive (like carname).

Rules for Naming Variables in VBScript

Must start with a letter

Cannot contain a dot (.)

Must not exceed 255 characters

VBScript uses a Variant data type, which means a variable can store different kinds of data.

Declaring (Creating) Variables

 

Declaring a variable means creating it before use.

VBScript variables can be declared using:

Dim

Public

Private

Example:

 

Dim x Dim carname

You can also create a variable by assigning a value directly:

 

carname = "Volvo"

However, this method is not recommended, because misspelling a variable name can accidentally create a new variable and cause errors.

Using Option Explicit

Using Option Explicit

To avoid accidental variable creation due to spelling mistakes, VBScript provides Option Explicit.

This statement forces you to declare all variables before using them.

Example:

 

Option Explicit Dim carname carname = "Volvo"

Assigning Values to Variables

 

Values are assigned using the equal sign (=):

 

carname = "Volvo" x = 10

The variable name appears on the left side, and the assigned value is on the right.

VBScript Array Variables

 

An array variable stores multiple values in one variable.

Declaring an Array

 

Dim names(2)

Since indexing starts from zero, this array contains 3 elements.

Assigning Values to Array Elements

 

names(0) = "Tove" names(1) = "Jani" names(2) = "Stale"

Retrieving Values from an Array

 

mother = names(0)

Multidimensional Arrays

 

VBScript supports up to 60 dimensions in an array.

Example of a two-dimensional array with rows and columns:

 

Dim table(4,6)

Example of a Two-Dimensional Array

 

<% Dim x(2,2) x(0,0)="Volvo" x(0,1)="BMW" x(0,2)="Ford" x(1,0)="Apple" x(1,1)="Orange" x(1,2)="Banana" x(2,0)="Coke" x(2,1)="Pepsi" x(2,2)="Sprite" For i = 0 To 2    Response.Write("

")    For j = 0 To 2        Response.Write(x(i,j) & "
")    Next    Response.Write("

") Next %>

Lifetime of Variables

 

A variable declared outside a procedure can be accessed anywhere within the ASP file.

A variable declared inside a procedure exists only while that procedure runs and cannot be accessed outside it.

To share data across multiple ASP files, use Session variables or Application variables.

Session Variables

 

Session variables store information related to a single user and are accessible across all pages of the application.

Common uses include:

User name

User ID

User preferences

Application Variables

 

Application variables store data that is shared among all users of an application.

These are typically used for:

Global counters

Application-wide settings

Shared configuration data

 

Technology
Asp.net Classic
want to connect with us ?
Contact Us