Docstoc

it420_asp_ch4

Document Sample
it420_asp_ch4 Powered By Docstoc
					USING VARIABLES WITH ASP


   Colorado Technical University
              IT420
           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.


                                       ASP-4-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.402823E38 to -1.401298E-45 and 1.401298E-45 to
  3.402823E38)
• Double (-1.79769313486232E308 to
   -4.94065645841247E-324 and 4.94065645841247E-324 to
     1.79769313486232E308)
• Currency (Accepts up to four decimal places
   -922,337,203,685,477.5808 to 922,337,203,685,477.5808)
                                                   ASP-4-3
                                      Subtypes
• String - Contains text. CarType=“Vet”
• Date - DateTime = #08/23/2000#
• Boolean - (Always True [0] or False [-1])
   – binBoolean = False
• Empty - uninitialized variables
• NULL - this means nothing is in the variable
• Object - Blocks of code which accomplish things.
  (These will be discussed later)
                                              ASP-4-4
              Determining SubTypes
• To determine the subtype of a variant, use
  TypeName()
• Example code:
  Age_of_Instructor = 26
  TypeofVar = TypeName(Age_of_Instructor)


When run, TypeofVar would equal “Integer”

                                            ASP-4-5
               TypeName() Example
<HTML>
<HEAD>
<TITLE>Using TypeName</TITLE>
</HEAD>
<BODY>
<%
 Dim dblPi, varWhatIsPi, datToday, whatIsDate, strText, whatIsText
 dblPi = 3.142
 varWhatIsPi = TypeName(dblPi)

 datToday = #09/09/99#
 whatIsDate = TypeName(datToday)

 strText = "Hello World"
 whatIsText = TypeName(strText)

 Dim emp
 emptyVar = TypeName(emp)
%>

<P><B> dblPi returns <%= varWhatIsPi %></P>
<P>datToday returns <%= whatIsDate %></P>
<P>strText returns <%= whatIsText %></P>
<P>emp returns <%= emptyVar %></B></P>
</BODY>
</HTML>                                                              ASP-4-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.                         ASP-4-7
Variable Naming Convention
  •   Boolean     bin
  •   Byte        byt
  •   Date/Time   dat
  •   Double      dbl
  •   Integer     int
  •   Long        lng
  •   Object      obj
  •   Single      sng
  •   String      str

                        ASP-4-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.

                                           ASP-4-9
 Assignment and Comparison Operators

• Number1 = 2 (= is an assignment operator)
• Example Operation:
  Number1 =1
  Number1 = Number1 + 2
• If Number1 = 2 Then (= is a comparison
  operator)


                                       ASP-4-10
                Comparison Operators

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

                                       ASP-4-11
                     Arithmetic Operators
•   Addition                         +
•   Subtraction                      -
•   Multiplication                   *
•   Division                         /
•   Exponentiation                   ^
•   Negation (e.g Negative Number)   -
•   Modulus                          MOD or \


                                         ASP-4-12
                Arithmetic Code Example
<%Option Explicit%>
<HTML>
<HEAD>
<TITLE>Declaring Variables</TITLE>
</HEAD>
<BODY>
<%
 Dim intEarn, intTax, intTotal
 intEarn = 150
 intTax = 20
 intTotal = intEarn - ((intEarn/100)*intTax)
%>

<B><P>Your total earnings after tax are $<% = intTotal %> </B></P>
</BODY>
</HTML>
                                                                     ASP-4-13
                     Logical Operators
• AND (If intNumber1 =1 AND intNumber2 =2
  then)
• OR (If intNumber1 =1 OR intNumber2 =2 then)
• NOT (If NOT Number1=1 Then)
• Precedence of the logical Operators is:
   1. NOT
   2. AND
   3. OR
                                          ASP-4-14
                 String Concatenation

• Strings are added together with the
  ampersand &
• Example:
  strConcatenate = “Tim” & “Peterson”
  Output would be TimPeterson
• To string variables together:
  strVarTrain = var1 & var2 & var3

                                        ASP-4-15
Conversion of Data Types
<%Option Explicit%>
<HTML>
<HEAD>
<TITLE>Converting Variants</TITLE>
</HEAD>
<BODY>

<%
 Dim strPi, dblPi, intPi,strPi2
 Dim varWhatIsPi1, varWhatIsPi2, varWhatIsPi3, varWhatIsPi4

 strPi = "3.142"
 varWhatIsPi1 = TypeName(strPi)
 dblPi = CDbl(strPi)
 varWhatIsPi2 = TypeName(dblPi)
 intPi = CInt(dblPi)
 varWhatIsPi3 = TypeName(intPi)
 strPi2 = CStr(intPi)
 varWhatIsPi4 = TypeName(strPi2)
%>

<P><B>Pi is a <%= varWhatIsPi1 %> and Pi returns <%= strPi %> </B></P>
<P><B>Pi is a <%= varWhatIsPi2 %> and Pi returns <%= dblPi %> </B></P>
<P><B>Pi is a <%= varWhatIsPi3 %> and Pi returns <%= intPi %> </B></P>
<P><B>Pi is a <%= varWhatIsPi4 %> and Pi returns <%= strPi2 %> </B></P>
</BODY>                                                                   ASP-4-16
</HTML>
                        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.
                                        ASP-4-17
Variable Scope Example
<HTML>
<HEAD>
<TITLE>Using Script Level Variables</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<%
 strGlobal = "I'm a persistent script-level variable"
 Response.Write strGlobal
Sub Procedure_1
  strDifferent = "Hi I'm strDifferent in Procedure 1"
  Response.Write strdifferent
  Response.Write "<P>" & strGlobal & "</P>"
 End Sub
 Sub Procedure_2
  strDifferent = "Hi I'm strDifferent in Procedure 2"
  Response.Write strdifferent
  Response.Write "<P>" & strGlobal & "</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>
                                                         ASP-4-18
                 String Case Changes

• Change the case of a string using Ucase and
  Lcase
• Example:
  strText = “HeLlo”
  Response.Write Ucase(strText)
  Response.Write Lcase(strText)



                                        ASP-4-19
   Determining Length of a String

• The Length of a string can be ascertained by
  using Len(string).
• Example:
  strText = “Tim Peterson”
  intHowLong = Len(strText)
• intHowLong would contain the integer 12
• The count always begins at 1.

                                         ASP-4-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:
  strText = “Web Pages”
  strLeftChars = Left(strText, 3)
• strLeftChars would contain “Web”.
                                         ASP-4-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
  strText = HowLongIsAPieceOfString?”
  strMiddleChars = Mid(strText, 11, 5)
• In this example, strMiddleChars would
  contain “Piece”
                                          ASP-4-22
  Finding a String Within a String

• To find a word within a string, use
  InStr(string, text_to_be_located)
• Example:
  strText = “WhatIsMyName”
  intWhere = InStr(strText, “My”)
• intWhere will contain the integer 7
• inStr is case sensitive.

                                        ASP-4-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.

                                        ASP-4-24
                                Arrays

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


                                  ASP-4-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)

                                      ASP-4-26
          Multi-Dimensional Arrays

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

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:10/18/2012
language:English
pages:27