ASP Variables



 

Naming
Variables
Declaring
variables
Variable
Subtypes
checking
for a specific subtype
Converting
to a subtype

Variable is a section
of memory that can be used to store a piece of information.In Asp the informations
stored in a variable may be of different datatypes like numerical,text,date
etc. In most programming languages we should specify the datatype of variable
its dealing with.But the one important thing to know about variables in ASP
(VBScript) is that there is only one type: variant. A variant is a special
type of variable, which can store a value of any type.All variables are of type
variant. Each variable can have a different  subtype.




Naming Asp Variables:

The following points
to be remember while naming.

           – must start with
alphabetical character

           – can’t contain
any periods or type-declaration characters

           – must be unique
within the same scope (see below for a note about scope)

           – must be 255 characters
or less

Top




How to declare Asp variables:

Varient should
be declared before they are used withing the program.Thim is done with the keyword
Dim which is short for ‘dimension’.

 Dim
TestVar

Then
you could assign integer or string value to it.You do not specify the subtype
when you declare the variables.

Declaring a varient in this type is known as explicit
declaration.Aslo we can assign a value to varient without having first declared.This
process is known as implicit declaration of variables.

Option
explicit

If you add this
line (option explicit) to your program (Preferably at the begining) every
variable in the program must be explicitly declared before it is used.


Eg.

<SCRIPT
LANGUAGE VBSCRIPT>

Option Explicit

Dim strname

strname=”Cindy”

</script>

Top




Asp Variable Subtypes
 
 

Integer Simple whole
numbers within the range -32,768 to 32,767.Eg 32,-784
Byte Integers within
the range 0 to 255.It’s a useful type , because a variable can be easily
stored

by computer within a single byte, which is computer’s basic storage unit.
Long It is very
similar to integer type which supports a much larger range.range is from
-2,147,483,648 to  2,147,483,647.
Single This can hold
single precision floating point numbers within the range -3.402823E38 to


-1.401298E-45 for negative values;1.401298E-45 to 3.402823E38 for positive
values.
Double This hold
s double precision floating point numbers.The range is -1.79769313486232E308
to

-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to1.79769313486232E308
for positive values.
String This can hold
pure text , or a mixture of text and numerical data and even date information.


Strings can range in length from 0 to 2 billion characters. Eg.”Myname”
, “4.9L” ,

“Feb 28 ,2000” .
Date This can be
used to hold  dates and times.The date must be surrounded by # sybols.Otherwise
it will be considered as a numerical expression. Eg. #10/15/00# ,#12.30#.
Boolean This can be
a set of one of two values TRUE or FALSE.
Currency The range
is -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Empty
Null
Object

Top




Checking for a specific variable subtype:

1)In asp One method
for checking variable subtype is by using function TypeName().

Syntax is :

          TypeName(VariableToCheck)

Eg:

<html>

<head><title>Type name</title></head>


<body>

<%

Dim vardate,vardatetype, strtext,strtexttype

vardate=now

vardatetype = Typename(vardate)

strtext
= “Hello”

strtexttype = TypeName(strtext)

%>

<p>vardate
returns <%= vardatetype%> </p>

<p>strtext returns <%=strtesttype%> </p>

</body>

</html>

 

The
result is:

vardate
returns Date

varstrtest
returns string

 

2)We
can check the subtype using the follwing functions.

 
 

IsEmpty(VariableToCheck) return a boolean
value (true or false). If the variable has been created, but has not been
assigned a value yet it is considered to be empty. This is different from
a value that is empty (“”) or equal to NULL.
IsNull(VariableToCheck) return a boolean
value (true or false). A variable is null only when it equals null. You
can set a variable equal to null.
IsNumeric(VariableToCheck) return a boolean
value (true or false). This will return true if the variable can be converted
to a numeric SubType without generating an error.
IsArray(VariableToCheck) return a boolean
value (true or false). 
IsDate(VariableToCheck) return a boolean
value (true or false). 
IsObject(VariableToCheck) return a boolean
value (true or false). 

Eg:

        <%


         
myVar = “Hello”

          
If IsNumeric(myVar) Then

               
Response.Write(“Variable is numeric.”)

          
Else      Response.Write(“Variable is NOT numeric.”)


          
End If

          
%>

          
Result:

         Variable
is NOT numeric.

Top




Converting asp variables to a specific subtype:

VBScript
provides number of functions to convert the value of a varient from one subtype
to other subtype.

 
 

Functions Description
CBool Converts
a variable into varient with Boolean Subtype
CByte Converts
a variable into varient with Byte Subtype
CCur Converts
a variable into varient with Currency Subtype
CDate Converts
a variable into varient with Date Subtype
CDbl Converts
a variable into varient with Double Subtype
CInt Converts
a variable into varient with Integer Subtype
CSng Converts
a variable into varient with Single Subtype
CStr Converts
a variable into varient with String Subtype
DateSerial Converts
a variable into varient with Date Subtype for a given year,month,day
DateValue returns a
value representing a varient of subtype Date
Timeserial Converts
a variable into varient with Date Subtype for a given hour,minute,second
TimeValue returns a
value representing a varient of subtype Date
Hex returns a
string containing hexadecimal value of a number
Oct returns a
string containing octal value of a number
Fix returns integer
portion of a number
Int Also returns
integer portion of a number
Sng returns sign
(+ve  or -ve) of a number
Abs returns absolute
value of a number

Top



Asp Local and Global variables

Top



 

 

Leave a Comment