1 Chapter 24 VBScript Outline 24 1 24
1 Chapter 24 - VBScript Outline 24. 1 24. 2 24. 3 24. 4 24. 5 24. 6 24. 7 24. 8 24. 9 24. 10 Introduction Operators Data Types and Control Structures VBScript Functions VBScript Example Programs Arrays String Manipulation Classes and Objects Operator Precedence Chart Internet and World Wide Web Resources 2001 Prentice Hall, Inc. All rights reserved.
2 24. 2 Operators 2001 Prentice Hall, Inc. All rights reserved.
3 24. 2 Operators 2001 Prentice Hall, Inc. All rights reserved.
4 24. 3 Data Types and Control Structures 2001 Prentice Hall, Inc. All rights reserved.
5 24. 3 Data Types and Control Structures 2001 Prentice Hall, Inc. All rights reserved.
6 24. 3 Data Types and Control Structures 2001 Prentice Hall, Inc. All rights reserved.
7 24. 3 Data Types and Control Structures 2001 Prentice Hall, Inc. All rights reserved.
1 2 3 4 ’ VBScript For y = 2 To 20 Step 2 Call Msg. Box( "y = " & y ) Next Outline Fig. 24. 11 Using keyword Step in VBScript’s For repetition structure with keyword Step 2001 Prentice Hall, Inc. All rights reserved. 8
9 24. 4 VBScript Functions 2001 Prentice Hall, Inc. All rights reserved.
10 24. 4 VBScript Functions 2001 Prentice Hall, Inc. All rights reserved.
11 24. 4 VBScript Functions 2001 Prentice Hall, Inc. All rights reserved.
12 24. 4 VBScript Functions 2001 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > <!--Fig. 24. 15: addition. html --> <!--Adding Integers --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Our first VBScript</title> <script type = "text/vbscript"> <!-Option Explicit Dim int. Total Sub cmd. Add_On. Click() Dim int. Value Set type to VBScript. Option Explicit statement. Define procedure On. Click for the Addition. html cmd. Add button. int. Value = Input. Box(_ "Enter an integer", "Input Box", , 1000) int. Total = CInt( int. Total ) + CInt( int. Value ) Call Msg. Box("You entered " & int. Value & _ "; total so far is " & int. Total, , "Results") End Sub --> </script> Use CInt to convert </head> input values from string subtype to integer subtype. <body> Click the button to add an integer to the total. <hr /> <form action = ""> <input name = "cmd. Add" type = "button" value = "Click Here to Add to the Total" /> 2001 Prentice Hall, Inc. All rights reserved. 13
36 37 38 </form> </body> </html> Outline Addition. html Program Output 2001 Prentice Hall, Inc. All rights reserved. 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > <!-- Fig. 24. 16: site. html --> <!-- Displaying a Web site --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Select a site to browse</title> </head> <body> Select a site to browse<p> <hr /> <form action = ""> <select name = "Site. Selector" size = "1"> Create form with select component. <option value = "http: //www. deitel. com"> Deitel & Associates, Inc. </option> Site. html <option value = "http: //www. prenhall. com"> Prentice Hall </option> The event attribute indicates the event to which the script responds (On. Change). "http: //www. phptr. com/phptrinteractive"> <option value = Prentice Hall Interactive </option> </select> The <script> tag’s for attribute indicates Script response to user’s selecting an the XHTML component on which the script option in the menu operates (Site. Selector). <!-- VBScript code --> <script for = "Site. Selector" event = "onchange" type = "text/vbscript"> 2001 Prentice Hall, Inc. All rights reserved. 15
36 37 38 39 40 41 42 <!-Document. Location = Document. Forms( 0 ). Site. Selector. Value --> </script> </form></p> </body> </html> Outline Site. html Program Output 2001 Prentice Hall, Inc. All rights reserved. 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > <!--Fig. 24. 17: minimum. html --> <!-- VBScript Procedures --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Using VBScript Procedures </title> Define procedures Minimum and Odd. Even. <script type = "text/vbscript"> <!-Option Explicit ' Find the minimum value. Assume that first value is ' the smallest. Function Minimum( min, a, b ) Single-line comment. If a < min Then min = a End If If b < min Then min = b End If Minimum = min End Function Minimum. html Use modulus operator to determine whether number odd or even. ' Return value Sub Odd. Even( n ) If n Mod 2 = 0 Then Call Msg. Box( n & " is the smallest and is even" ) 2001 Prentice Hall, Inc. All rights reserved. 17
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 Else Call Msg. Box( n & " is the smallest and is odd" ) End If End Sub Outline Sub cmd. Button_On. Click() Dim number 1, number 2, number 3, smallest ' Convert number 1 = number 2 = number 3 = each input to Long subtype CLng( Document. Forms( 0 ). txt. Box 1. Value ) CLng( Document. Forms( 0 ). txt. Box 2. Value ) CLng( Document. Forms( 0 ). txt. Box 3. Value ) smallest = Minimum( number 1, number 2, number 3 ) Call Odd. Even( smallest ) End Sub --> Define an event procedure </script> cmd. Button’s On. Click </head> Call function Minimum. for handling event. Minimum. html Pass the smallest number to procedure Odd. Even. <body> <form action = ""> Enter a number <input type = "text" name = "txt. Box 1" size = "5" value = "0" /> <p>Enter a number <input type = "text" name = "txt. Box 2" size = "5" value = "0" /></p> <p>Enter a number <input type = "text" name = "txt. Box 3" size = "5" value = "0" /></p> <p><input type = "button" name = "cmd. Button" value = "Enter" /></p> 2001 Prentice Hall, Inc. All rights reserved. 18
67 68 69 </form> </body> </html> Outline Minimum. html Program Output 2001 Prentice Hall, Inc. All rights reserved. 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > Outline <!--Fig. 24. 18: arrays. html --> <!--VBScript Arrays --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Using VBScript Arrays</title> <script type = "text/vbscript"> <!-Option Explicit Public Sub Display. Array( x, s ) Dim j Document. Write( s & ": " ) For j = 0 To UBound( x ) Document. Write( x( j ) & " " ) Next Document. Write( " " ) End Sub Define procedure Display. Array. Function UBound returns the upper bound (i. e. , the highest-numbered index). Initialize arrays. Arrays. html Statement Re. Dim allocates memory for array dynamic. Dim fixed. Size( 3 ), fixed. Array, dynamic(), k Re. Dim dynamic( 3 ) ' Dynamically size array fixed. Array = Array( "A", "B", "C" ) ' Populate arrays with values For k = 0 to UBound( fixed. Size ) fixed. Size( k ) = 50 - k dynamic( k ) = Chr( 75 + k ) Function array takes any number of arguments and returns an array containing those arguments. 2001 Prentice Hall, Inc. All rights reserved. 20
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 Outline Next ' Display contents Call Display. Array( of arrays fixed. Size, "fixed. Size" ) fixed. Array, "fixed. Array" ) dynamic, "dynamic" ) ' Resize dynamic, preserve current values Re. Dim Preserve dynamic( 5 ) dynamic( 3 ) = 3. 343 Call procedure dynamic( 4 ) = 77. 37443 Arrays. html Display. Array. Call Display. Array( dynamic, _ "dynamic after Re. Dim Preserve" ) --> </script> Reallocate dynamic’s memory to 5 elements. </head><body></body> Keyword Preserve, when used with Re. Dim, </html> maintains the current values in the array. Program Output 2001 Prentice Hall, Inc. All rights reserved. 21
22 24. 7 String Manipulation 2001 Prentice Hall, Inc. All rights reserved.
23 24. 7 String Manipulation 2001 Prentice Hall, Inc. All rights reserved.
24 24. 7 String Manipulation 2001 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > <!--Fig. 24. 20: piglatin. html --> <!-- VBScript String Functions --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Using VBScript String Functions Define </title> Function <script type = "text/vbscript"> <!-Option Explicit procedure Translate. To. Pig. Latin Public Function Translate. To. Pig. Latin( english. Phrase ) Dim words ' Stores each individual word Dim k, suffix Split ' Get each word and store in words the ' default delimiter for Split is a space words = Split( english. Phrase ) phrase Piglatin. html into words Convert each word to pig Latin For k = 0 To UBound( words ) ' Check if first letter is a vowel If In. Str( 1, "aeiou", _ LCase( Left( words( k ), 1 ) ) ) Then suffix = "y" Else Function LCase returns a a Function In. Str searches suffix = "ay" lowercase string. End If string for a substring. 2001 Prentice Hall, Inc. All rights reserved. 25
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 Outline ' Convert the word to pig Latin words( k ) = Right( words( k ), _ Len( words( k ) ) - 1 ) & _ Left( words( k ), 1 ) & suffix Next Function Len returns the number of Function Right returns a string ' Return translated phrase, each word Function Left returns characters in a string. ' is separated by spaces containing characters from the Translate. To. Pig. Latin = Join( words ) characters from the left side of containing right side of a string argument. End Function a string argument. Sub cmd. Button_On. Click() Dim phrase Return translated phrase using Join function phrase = Document. Forms( 0 ). txt. Input. Value Document. forms( 0 ). txt. Pig. Latin. Value = _ Translate. To. Pig. Latin( phrase ) End Sub --> </script> Define an event </head> Piglatin. html procedure for cmd. Button’s On. Click event. <body> <form action = ""> Enter a sentence <input type = "text" name = "txt. Input" size = "50" /> <p>Pig Latin <input type = "text" name = "txt. Pig. Latin" size = "70" /> </p><p> <input type = "button" name = "cmd. Button" value = "Translate" /></p> </form> </body> </html> 2001 Prentice Hall, Inc. All rights reserved. 26
Outline Program Output 2001 Prentice Hall, Inc. All rights reserved. 27
1 2 3 4 5 6 7 8 9 Outline Private m. Hour Public Property Let Hour( hr ) If hr >= 0 And hr < 24 Then m. Hour = hr Else m. Hour = 0 End If End Property Fig. 24. 21 Simple Property Let procedure. A simple Property Let procedure 2001 Prentice Hall, Inc. All rights reserved. 28
1 2 3 Outline Public Property Get Hour() Hour = m. Hour End Property Fig. 24. 22 Simple Property Get procedure. A simple Property Get procedure 2001 Prentice Hall, Inc. All rights reserved. 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Outline Class CTime 1 Private m. Hour Public Property Let Hour( hr ) If hr >= 0 And hr < 24 Then m. Hour = hr Else m. Hour = 0 End If End Property A simple Class definition Public Property Get Hour() Hour = m. Hour End Property End Class Fig. 24. 23 Simple Class definition. 2001 Prentice Hall, Inc. All rights reserved. 30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -transitional. dtd" > <!--Fig. 24: classes. html --> <!-- VBScript Class --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Using a VBScript Class</title> <script type = "text/vbscript"> <!-Option Explicit Define Class Person Private name, years. Old, ssn Public Property Let First. Name( fn ) name = fn End Property Define Property Let and Property Get procedures Classes. html Public Property Get First. Name() First. Name = name End Property Public Property Let Age( a ) years. Old = a End Property Define Property Let Social. Security. Number Public Property Get Age() Age = years. Old End Property Public Property Let Social. Security. Number( n ) 2001 Prentice Hall, Inc. All rights reserved. 31
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline If Validate( n ) Then ssn = n Else ssn = "000 -00 -0000" Call Msg. Box( "Invalid Social Security Format" ) End If End Property Call function Validate Public Property Get Social. Security. Number() Social. Security. Number = ssn End Property Define Function Validate Private Function Validate( expression ) Dim regular. Expression Set regular. Expression = New Reg. Exp regular. Expression. Pattern = "^d{3}-d{2}-d{4}$" If regular. Expression. Test( expression ) Then Validate = True Else Use regular expression Validate = False End If End Function Classes. html to check format Define Function To. String. Public Function To. String() To. String = name & Space( 3 ) & age & Space( 3 ) _ & ssn End Function End Class ' Person 2001 Prentice Hall, Inc. All rights reserved. 32
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Sub cmd. Button_On. Click() Dim p Set p = New Person Outline ' Declare object reference ' Instantiate Person object With p. First. Name = Document. Forms(0). txt. Box 1. Value. Age = CInt( Document. Forms(0). txt. Box 2. Value ). Social. Security. Number =_ Document. Forms( 0). txt. Box 3. Value Call Msg. Box(. To. String() ) End With End Sub --> </script> </head> Provide an event procedure for Instantiate Person object. cmd. Button’s Use the With/End With. On. Click statementevent. to set several property values for p and call p’s To. String method. <body> <form action = "">Enter first name <input type = "text" name = "txt. Box 1" size = "10" /> <p>Enter age <input type = "text" name = "txt. Box 2" size = "5" /></p> <p>Enter social security number <input type = "text" name = "txt. Box 3" size = "10" /> </p><p> <input type = "button" name = "cmd. Button" value = "Enter" /></p> </form> </body> </html> Classes. html 2001 Prentice Hall, Inc. All rights reserved. 33
Outline Program Output 2001 Prentice Hall, Inc. All rights reserved. 34
35 24. 9 Operator Precedence Chart 2001 Prentice Hall, Inc. All rights reserved.
- Slides: 35