Codefixer - ASP tutorials, resources and software
ASP.NET Hosting with US & UK Data Centers!
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

New MS SQL 2008 Available – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Variables in ASP (VBScript)

A variable acts as a container holding or storing a value in the server computer's memory. We can then use or change the value of the variable throughout our code. Thus we could create a variable myFavouriteTeam that holds my favourite football team namely 'Spurs' and print out the value of the variable on our page. Note that to declare a variable in ASP/VBScript we use the Dim statement.

Example

<%
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
%>

In ASP/VBScript there is only one datatype, and that is variant. A variant can hold either strings or numbers, and will act as a number if its numeric, and as a string for string values.

In ASP/VBScript, it is not necessary to declare your variables before using them though it is good practice. You must use the Dim statement to declare variables as shown in the example below. You should also have a look at the Option Explicit tutorial which relates to variable declaration.

Here is an example where we assign a value to a couple of variables and then use them in the script.

<%@ Language="VBScript" %>
<% Option Explicit %>
<%
'Commented lines starting with an apostrophe are not executed
'Declare your variables
Dim temperature, country
temperature=28
country="Ireland"

response.write "The temperature is " & temperature & " in " & country
%>

The output is: The temperature is 28 in Ireland

The variable value will be kept in memory for the life span of the current page and will be released from memory when the page has finished executing.
In naming variables in VBScript you must be aware of these rules;

  • Variables must begin with a letter not an underscore or a number
  • They cannot have more than 255 characters
  • They cannot contain a period (full stop) , a space or a dash
  • You can use an underscore though not as the first character
  • If you use VBscript then case sensitivity is not important.

9variable   'is not a valid variable name as it begins with a number
_variable   'is not a valid variable name as it begins with an underscore
variable. 'is not a valid variable as there is a period (full stop)
variable 'both these variables in red are the same when using VBscript
VARIABLE

If the name of your variable breaks the rules then you'll encounter a fairly obvious error message like the one below.

Invalid Character

Also note in the case above that Strings require quotes and numeric values don't. With the example above you can see that tempature=28 doesn't have quotes whereas country="Ireland" does.

Read our related tutorial on Variables and Constants

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software