VKS-LEARNING HUB

Home » Class XII » MMWT-XII » ASP-DATATYPE

ASP-DATATYPE

DATA TYPE IN ASP

Variant is a special data type which can hold any type of data.

Sub Type of VARIANT : Integer, Byte, Double, Long, String, Date, Currency, Float

Variable is name of Memory space to hold value

Variable can be declare in two ways: Implicit declaration & Explicit declaration

Implicit Declaration : We can define variable anywhere in the program by assigning a value to a variable.

Example 1: <%A=10%> means we have declared a variable A which holds the value 10

which is integer

Example 2: <% A=”WEB-TECH” %>means we have declared a variable A which holds the value “WEB-TECH” which is String

 

Explicit Declaration : We can define variable anywhere in the program by using a keyword DIM. In Explicit Declaration first the variable is declare using DIM statement and then assigned any value. We cannot declare and assigned a value at the same time

i.e Dim A=10 is wrong declaration

Correct Way :  <% Dim A

A=10%>

Example

<HTML>

<TITLE>This is an example</TITLE>

<BODY>

<%’BEGIN ASP SCRIPT

Dim cVariable ‘Initialize a character variable

cVariable = “This is a variable in ASPScript”

response.write cVariable

%>

<!–END ASP SCRIPT ––>

</BODY>

</HTML>

 

The HTML code that is sent to the browser will be:

<HTML>

<TITLE>This is an example</TITLE>

<BODY>

This is a variable in ASPScript

</BODY>

</HTML>

And the browser will display:

This is a variable in ASPScript

Note the use of standard HTML tags (HTMLTITLEBODY), the ASP script bounded by its delimiters <% and %>, and the two styles of comments. The ASP interpreter will evaluate every expression between the delimiters. The client browser displays the results and messages that are generated from the ASP script.
The following example performs a calculation and displays the result by inserting a calculated value into a line of text.

<HTML>

<TITLE>Applied Discount</TITLE>

<BODY>

<%             ‘BEGIN ASP SCRIPT

Dim nTotal     ‘Initialize a numeric variable

nTotal = 20    ‘Set this numeric variable to a value

%>             <!–END ASP SCRIPT ––>

Your $<%=nTotal%> total with a 10% discount is $<%=nTotal

– (nTotal * .10)%>!

</BODY>

</HTML>

Output Value

Placing an equals sign after the starting ASP script delimiter (<%=) is one way to direct ASP to output a value or text string. We will discuss ways to write in ASP in a later lesson.

The output from this example will be:

Your $20 total with a 10% discount is $18!

 

Option Explicit

Option Explicit requires that all variable names be declared (with the Dim statement). Using Option Explicit to force variable declaration is good for a number of reasons. First, let’s look at this code example:

<%
MyName = “michael”
Response.Write(MyNam)

%>

As I have mispelt MyName and written MyNam nothing will be printed out, ASP will create a new variable MyNam and assign an empty string to it. If your code is long then finding this typing mistake can take you hours.Trust me!!

<%
Option Explicit ‘Always, always, always!
‘declare your variables
Dim MyName
MyNam = “Michael”
Response.Write(MyName)

%>

Note that If you write the code in the style above using Option Explicit you will get an ASP error informing you that MyNam is undefined. You must first use the Dim statement. Debugging becomes alot easier!

Arrays

VBScript also allows for the use of arrays. Note the use of the variable names in this code segment to identify them as a(rrays) of i(nteger) and (character) data:

<HTML>

<%     ‘BEGIN ASP SCRIPT

Dim aiData(2,3)

‘Initialize a 2×3 array of integer values

Dim acCharData(5,4)

‘Initialize a 5×4 array of character values

%>

<!–END ASP SCRIPT ––>

Two Ways to Declare Array in ASP

<%

Dim myFixedArray(3) ‘Fixed size array

Dim myDynArray() ‘Dynamic size array

%>

Assigning values to an array

<%

Dim myFixedArray(3) ‘Fixed size array

myFixedArray(0) = “Albert Einstein”

myFixedArray(1) = “Mother Teresa”

myFixedArray(2) = “Bill Gates”

myFixedArray(3) = “Martin Luther King Jr.”

%>

 

<%

Dim myFixedArray(3) ‘Fixed size array

myFixedArray(0) = “Albert Einstein”

myFixedArray(1) = “Mother Teresa”

myFixedArray(2) = “Bill Gates”

myFixedArray(3) = “Martin Luther King Jr.”

For Each item In myFixedArray

Response.Write(item & “<br />”)

Next

%>

Display:

Albert Einstein Mother Teresa Bill Gates Martin Luther King Jr.

ASP dynamic sized arrays

To create an array whose size can be changed at any time simply do not put a number within the parenthesis when you declare the array. When you know what size you want the array to be use the ReDim keyword. You may ReDim as many times as you wish.

If you want to keep your data that already exists in the array then use thePreserve keyword.

<%

Dim myDynArray() ‘Dynamic size array

ReDim myDynArray(1)

myDynArray(0) = “Albert Einstein”

myDynArray(1) = “Mother Teresa”

ReDim Preserve myDynArray(3)

myDynArray(2) = “Bill Gates”

myDynArray(3) = “Martin Luther King Jr.”

For Each item In myDynArray

            Response.Write(item & “<br />”)

Next

%>

ARRAY FUNCTIONS

The UBound() Function

This function will return the ‘index’ of the highest element in an array. The index is basically the position of the element in the array. Arrays in ASP/VBScript have a zero based starting index.

Syntax: UBound(ArrayName)
– This function will return the highest element available in an array.
– If your array has 10 elements (remember zero base) then the upper bound would be 9.

Example:

<%
Dim myArray

myArray(0)=”MMWT”
myArray(1)=”Chemisry”
myArray(2)=”Physics”
myArray(3)=”Biology”
highest_element=UBound(myArray)
response.write highest_element

%>

Output: 3

The LBound() Function

Syntax: LBound(ArrayName)
– This function will return the lowest element available in an array, in most cases 0.

Example:

<%
Dim myArray(3)

myArray(0)=”MMWT”
myArray(1)=”Chemisry”
myArray(2)=”Physics”
myArray(3)=”Biology”

lowest_element=LBound(myArray)
response.write lowest_element

%>

Output: 0

The Split() Function

The Split function is used to split (break up) a string of characters into an array.

Syntax: Split(String, Delimiter, Count)
– String is the string we will be splitting

– Delimiter is the character that you want the string to be split by.
– Count is the number of elements you want to limit the array to and is optional.

Example:

<%
Dim MyString, MyArray

MyString = “MMWT,Chemistry,Physics,Biology”

MyArray = Split(MyString,”,”) ‘the delimiter is the comma

%>

The Result would be an array called MyArray with 4 elements.

myArray(0)=”MMWT”
myArray(1)=”Chemisry”
myArray(2)=”Physics”
myArray(3)=”Biology”

Again we could loop through the array and print out the values in each array element. In this example we’ll also incorporate the UBound function.

<%
Dim MyString, MyArray

MyString = “MMWT,Chemistry,Physics,Biology”

MyArray = Split(MyString,”,”)

For i=0 to UBound(MyArray) ‘the UBound function returns 3

response.write myArray(i) & “<br>”

Next ‘move on to the next value of i

%>

Notice that in the code above we used the UBound Function which returns the index of the highest element in the array. In this case it’s 3 so the For i loop will loop from 0 to 3.


Leave a comment