USING VARIABLES WITH ASP Colorado Technical University IT

USING VARIABLES WITH ASP Colorado Technical University IT 420 Tim Peterson ASP-4 -1

What is a Variable • Section of memory allocated by a name. • This memory is used to store data. • When the Dim statement is used, memory is reserved. • Always initialize variables. • Variables in VBScript are variants. 2

Numeric Subtypes • • Integer (-32, 767 to 32, 767) Byte (Integer with the range of 0 to 255) Long (-2, 147, 483, 648 to 2, 147, 483, 647) Single (-3. 402823 E 38 to -1. 401298 E-45 and 1. 401298 E-45 to 3. 402823 E 38) • Double (-1. 79769313486232 E 308 to -4. 94065645841247 E-324 and 4. 94065645841247 E-324 to 1. 79769313486232 E 308) • Currency (Accepts up to four decimal places -922, 337, 203, 685, 477. 5808 to 922, 337, 203, 685, 477. 5808) 3

Subtypes • String - Contains text. Car. Type=“Vet” • Date - Date. Time = #08/23/2000# • Boolean - (Always True [0] or False [-1]) – bin. Boolean = False • Empty - uninitialized variables • NULL - this means nothing is in the variable • Object - Blocks of code which accomplish things. (These will be discussed later) 4

Determining Sub. Types • To determine the subtype of a variant, use Type. Name() • Example code: Age_of_Instructor = 26 Typeof. Var = Type. Name(Age_of_Instructor) When run, Typeof. Var would equal “Integer” 5

Type. Name() Example <HTML> <HEAD> <TITLE>Using Type. Name</TITLE> </HEAD> <BODY> <% Dim dbl. Pi, var. What. Is. Pi, dat. Today, what. Is. Date, str. Text, what. Is. Text dbl. Pi = 3. 142 var. What. Is. Pi = Type. Name(dbl. Pi) dat. Today = #09/09/99# what. Is. Date = Type. Name(dat. Today) str. Text = "Hello World" what. Is. Text = Type. Name(str. Text) Dim empty. Var = Type. Name(emp) %> <P><B> dbl. Pi returns <%= var. What. Is. Pi %></P> <P>dat. Today returns <%= what. Is. Date %></P> <P>str. Text returns <%= what. Is. Text %></P> <P>emp returns <%= empty. Var %></B></P> </BODY> </HTML> 6

Variable Naming • • Variables should clearly state what they contain Avoid confusing abbreviations Never use the same variable name twice For functions or procedures, start their name with f_ or p_ Variables are limited to 255 characters. Variables must start with a letter Avoid all symbols with the excpetion of _ and -. Pay attention to case. 7

Variable Naming Convention • • • Boolean Byte Date/Time Double Integer Long Object Single String bin byt dat dbl int lng obj sng str 8

Option Explicit • Option Explicit forces the declaration of variables. • Always make it the first line in an ASP file. • If not the first line, an error will be generated. • Reason: Scripts are run from top to bottom and ASP is processed prior to HTML. 9

Assignment and Comparison Operators • Number 1 = 2 (= is an assignment operator) • Example Operation: Number 1 =1 Number 1 = Number 1 + 2 • If Number 1 = 2 Then (= is a comparison operator) 10

Comparison Operators • • • Equality Less than or equal to Inequality (Does not equal) Greater than or equal to = < <= <> > >= 11

Arithmetic Operators • • Addition Subtraction Multiplication Division Exponentiation Negation (e. g Negative Number) Modulus + * / ^ MOD or 12

Arithmetic Code Example <%Option Explicit%> <HTML> <HEAD> <TITLE>Declaring Variables</TITLE> </HEAD> <BODY> <% Dim int. Earn, int. Tax, int. Total int. Earn = 150 int. Tax = 20 int. Total = int. Earn - ((int. Earn/100)*int. Tax) %> <B><P>Your total earnings after tax are $<% = int. Total %> </B></P> </BODY> </HTML> 13

Logical Operators • AND (If int. Number 1 =1 AND int. Number 2 =2 then) • OR (If int. Number 1 =1 OR int. Number 2 =2 then) • NOT (If NOT Number 1=1 Then) • Precedence of the logical Operators is: 1. NOT 2. AND 3. OR 14

String Concatenation • Strings are added together with the ampersand & • Example: str. Concatenate = “Tim” & “Peterson” Output would be Tim. Peterson • To string variables together: str. Var. Train = var 1 & var 2 & var 3 15

Conversion of Data Types <%Option Explicit%> <HTML> <HEAD> <TITLE>Converting Variants</TITLE> </HEAD> <BODY> <% Dim str. Pi, dbl. Pi, int. Pi, str. Pi 2 Dim var. What. Is. Pi 1, var. What. Is. Pi 2, var. What. Is. Pi 3, var. What. Is. Pi 4 str. Pi = "3. 142" var. What. Is. Pi 1 = Type. Name(str. Pi) dbl. Pi = CDbl(str. Pi) var. What. Is. Pi 2 = Type. Name(dbl. Pi) int. Pi = CInt(dbl. Pi) var. What. Is. Pi 3 = Type. Name(int. Pi) str. Pi 2 = CStr(int. Pi) var. What. Is. Pi 4 = Type. Name(str. Pi 2) %> <P><B>Pi is a <%= var. What. Is. Pi 1 %> and Pi returns <%= str. Pi %> </B></P> <P><B>Pi is a <%= var. What. Is. Pi 2 %> and Pi returns <%= dbl. Pi %> </B></P> <P><B>Pi is a <%= var. What. Is. Pi 3 %> and Pi returns <%= int. Pi %> </B></P> <P><B>Pi is a <%= var. What. Is. Pi 4 %> and Pi returns <%= str. Pi 2 %> </B></P> </BODY> </HTML> 16

Variable Scope • Under certain conditions, variables of the same name are allowed (don’t do this). • Local variables are assigned in subprocedures. • Global variables are defined at the script level. • Subprocedures hold blocks of code that can be called from other parts of the program. 17

Variable Scope Example <HTML> <HEAD> <TITLE>Using Script Level Variables</TITLE> </HEAD> <BODY BGCOLOR="white"> <% str. Global = "I'm a persistent script-level variable" Response. Write str. Global Sub Procedure_1 str. Different = "Hi I'm str. Different in Procedure 1" Response. Write strdifferent Response. Write "<P>" & str. Global & "</P>" End Sub Procedure_2 str. Different = "Hi I'm str. Different in Procedure 2" Response. Write strdifferent Response. Write "<P>" & str. Global & "</P>" End Sub %> <P>Calling Procedure_1. . . <I><%Procedure_1()%></I></P> <P>Calling Procedure_2. . . <I><%Procedure_2()%></I></P> <P>Calling Procedure_1. . . <I><%Procedure_1()%></I></P> </BODY> </HTML> 18

String Case Changes • Change the case of a string using Ucase and Lcase • Example: str. Text = “He. Llo” Response. Write Ucase(str. Text) Response. Write Lcase(str. Text) 19

Determining Length of a String • The Length of a string can be ascertained by using Len(string). • Example: str. Text = “Tim Peterson” int. How. Long = Len(str. Text) • int. How. Long would contain the integer 12 • The count always begins at 1. 20

Extracting Characters • To extract chars from the beginning or end of a string, use Left(string, number_of_chars_to_remove) or Right (string, number_of_chars_to_remove) • Example: str. Text = “Web Pages” str. Left. Chars = Left(str. Text, 3) • str. Left. Chars would contain “Web”. 21

Extracting Characters - Cont’d • To extract from the middle of a string, use Mid(string, where_in_the_string, number_of_chars_to_extract) • Example str. Text = How. Long. Is. APiece. Of. String? ” str. Middle. Chars = Mid(str. Text, 11, 5) • In this example, str. Middle. Chars would contain “Piece” 22

Finding a String Within a String • To find a word within a string, use In. Str(string, text_to_be_located) • Example: str. Text = “What. Is. My. Name” int. Where = In. Str(str. Text, “My”) • int. Where will contain the integer 7 • in. Str is case sensitive. 23

Trimming Strings • Spaces can be removed from strings using: • Trim(string) - Removes all spaces from the beginning and end of a string. • Ltrim(string) - Removes spaces from the left side of a string. • Rtrim(string) - Removes spaces from the right side of a string. 24

Arrays • Arrays always start with 0. • Fixed array declaration: Dim str. Cousins(50) • Dynamic array declaration Dim str. Cousins() Redim str. Cousins(14) 25

Redeclaring Arrays • Arrays can be redeclared as such: Dim cousins() Redim cousins(1) cousins(0) = “Sue” cousins(1) = “Pam” Redim cousins(2) • Redim Preserve cousins(2) 26

Multi-Dimensional Arrays • Multi-Dimensional Arrays are declared as follows: Dim Tims. Array(5, 5) • To define components of the array: Tims. Array(0, 0) = “Tim” Tims. Array(1, 0) = “John” • Arrays can be declared with up to 60 dimensions. 27
- Slides: 27