Constants in ASP (VBScript)

In our 'Variable' tutorial we showed how a variable acts as a container holding or storing a value in the server computer's memory. Well so does a constant.
With a variable we can change the value of the variable throughout our code, with a constant we can't. Have a look at the code for example 1.

Example 1:

<%
Option Explicit
'declare your variable
Dim myFavouriteTeam
'assign a value to our variable
myFavouriteTeam="Spurs"
'the above variable myFavouriteTeam holds the value Spurs
response.write myFavouriteTeam
'the output will be Spurs
'assign a new value to our variable

myFavouriteTeam="Spurs Reserves"
'the above variable myFavouriteTeam now holds the value Spurs Reserves
response.write myFavouriteTeam
'Our browser will now display Spurs Reserves
%>

Note that the value of the variable myFavouriteTeam changes throughout the script from Spurs to Spurs Reserves.
The value stored in a constant cannot change throughout the life of a script or application after it has been declared.

To declare a const in VBScript we use the Const keyword. The constant is declared and assigned the value all on the same line.

Const MyConst=2

In we attempt to re-assign the value of the constant as we did with the value of the variable we'll get a run time error.

Also note in the case above that strings require quotes and numeric values don't as shown below.

Const MyConst=2
Const MyConst="myString"

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here