Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 24 - VBScript Outline 24.1 Introduction 24.2 Operators

Similar presentations


Presentation on theme: "Chapter 24 - VBScript Outline 24.1 Introduction 24.2 Operators"— Presentation transcript:

1 Chapter 24 - VBScript Outline 24.1 Introduction 24.2 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

2 24.2 Operators

3 24.2 Operators

4 24.3 Data Types and Control Structures

5 24.3 Data Types and Control Structures

6 24.3 Data Types and Control Structures

7 24.3 Data Types and Control Structures

8 For repetition structure with keyword Step
1 ’ VBScript 2 For y = 2 To 20 Step 2 Call MsgBox( "y = " & y ) 4 Next For repetition structure with keyword Step Fig Using keyword Step in VBScript’s For repetition structure.

9 24.4 VBScript Functions

10 24.4 VBScript Functions

11 24.4 VBScript Functions

12 24.4 VBScript Functions

13 Option Explicit statement.
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!--Fig : addition.html --> 6 <!--Adding Integers > 7 8 <html xmlns = " <head> <title>Our first VBScript</title> 11 <script type = "text/vbscript"> <!-- Option Explicit Dim intTotal 16 Sub cmdAdd_OnClick() Dim intValue 19 intValue = InputBox(_ "Enter an integer", "Input Box", , 1000, 1000) intTotal = CInt( intTotal ) + CInt( intValue ) Call MsgBox("You entered " & intValue & _ "; total so far is " & intTotal, , "Results") End Sub > </script> </head> 29 <body> Click the button to add an integer to the total. <hr /> <form action = ""> <input name = "cmdAdd" type = "button" value = "Click Here to Add to the Total" /> Addition.html Set type to VBScript. Option Explicit statement. Define procedure OnClick for the cmdAdd button. Use CInt to convert input values from string subtype to integer subtype.

14 Addition.html Program Output
</form> </body> 38 </html> Addition.html Program Output

15 Create form with select component.
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!-- Fig : site.html --> 6 <!-- Displaying a Web site --> 7 8 <html xmlns = " <head> <title>Select a site to browse</title> </head> 12 <body> Select a site to browse<p> <hr /> <form action = ""> <select name = "SiteSelector" size = "1"> 18 <option value = " Deitel & Associates, Inc. </option> 22 <option value = " Prentice Hall </option> 26 <option value = " Prentice Hall Interactive </option> 30 </select> 32 <!-- VBScript code --> <script for = "SiteSelector" event = "onchange" type = "text/vbscript"> Site.html Create form with select component. The event attribute indicates the event to which the script responds (OnChange). The <script> tag’s for attribute indicates the XHTML component on which the script operates (SiteSelector). Script response to user’s selecting an option in the menu

16 Site.html Program Output
<!-- Document.Location = Document.Forms( 0 ).SiteSelector.Value > </script> </form></p> </body> 42 </html> Site.html Program Output

17 Define procedures Minimum and OddEven.
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!--Fig : minimum.html --> 6 <!-- VBScript Procedures > 7 8 <html xmlns = " <head> <title>Using VBScript Procedures</title> 11 <script type = "text/vbscript"> <!-- Option Explicit 15 ' Find the minimum value. Assume that first value is ' the smallest. Function Minimum( min, a, b ) 19 If a < min Then min = a End If 23 If b < min Then min = b End If 27 Minimum = min ' Return value End Function 30 Sub OddEven( n ) If n Mod 2 = 0 Then Call MsgBox( n & " is the smallest and is even" ) Minimum.html Define procedures Minimum and OddEven. Single-line comment. Use modulus operator to determine whether number odd or even.

18 Define an event procedure for handling cmdButton’s OnClick event.
Else Call MsgBox( n & " is the smallest and is odd" ) End If End Sub 38 Sub cmdButton_OnClick() Dim number1, number2, number3, smallest 41 ' Convert each input to Long subtype number1 = CLng( Document.Forms( 0 ).txtBox1.Value ) number2 = CLng( Document.Forms( 0 ).txtBox2.Value ) number3 = CLng( Document.Forms( 0 ).txtBox3.Value ) 46 smallest = Minimum( number1, number2, number3 ) Call OddEven( smallest ) End Sub > </script> </head> 53 <body> <form action = ""> Enter a number <input type = "text" name = "txtBox1" size = "5" value = "0" /> <p>Enter a number <input type = "text" name = "txtBox2" size = "5" value = "0" /></p> <p>Enter a number <input type = "text" name = "txtBox3" size = "5" value = "0" /></p> <p><input type = "button" name = "cmdButton" value = "Enter" /></p> 66 Minimum.html Define an event procedure for handling cmdButton’s OnClick event. Call function Minimum. Pass the smallest number to procedure OddEven.

19 Minimum.html Program Output
</form> </body> 69 </html> Minimum.html Program Output

20 Define procedure DisplayArray.
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!--Fig : arrays.html --> 6 <!--VBScript Arrays > 7 8 <html xmlns = " <head> <title>Using VBScript Arrays</title> 11 <script type = "text/vbscript"> <!-- Option Explicit 15 Public Sub DisplayArray( x, s ) Dim j 18 Document.Write( s & ": " ) For j = 0 To UBound( x ) Document.Write( x( j ) & " " ) Next 23 Document.Write( "<br />" ) End Sub 26 Dim fixedSize( 3 ), fixedArray, dynamic(), k 28 ReDim dynamic( 3 ) ' Dynamically size array fixedArray = Array( "A", "B", "C" ) 31 ' Populate arrays with values For k = 0 to UBound( fixedSize ) fixedSize( k ) = 50 - k dynamic( k ) = Chr( 75 + k ) Arrays.html Define procedure DisplayArray. Function UBound returns the upper bound (i.e., the highest-numbered index). Initialize arrays. Statement ReDim allocates memory for array dynamic. Function array takes any number of arguments and returns an array containing those arguments.

21 Arrays.html Program Output
Next 37 ' Display contents of arrays Call DisplayArray( fixedSize, "fixedSize" ) Call DisplayArray( fixedArray, "fixedArray" ) Call DisplayArray( dynamic, "dynamic" ) 42 ' Resize dynamic, preserve current values ReDim Preserve dynamic( 5 ) dynamic( 3 ) = 3.343 dynamic( 4 ) = 47 Call DisplayArray( dynamic, _ "dynamic after ReDim Preserve" ) > </script> </head><body></body> 53 </html> Arrays.html Program Output Call procedure DisplayArray. Reallocate dynamic’s memory to 5 elements. Keyword Preserve, when used with ReDim, maintains the current values in the array.

22 24.7 String Manipulation

23 24.7 String Manipulation

24 24.7 String Manipulation

25 Define Function procedure TranslateToPigLatin
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!--Fig : piglatin.html --> 6 <!-- VBScript String Functions --> 7 8 <html xmlns = " <head> <title>Using VBScript String Functions</title> 11 <script type = "text/vbscript"> <!-- Option Explicit 15 Public Function TranslateToPigLatin( englishPhrase ) Dim words ' Stores each individual word Dim k, suffix 19 ' Get each word and store in words the ' default delimiter for Split is a space words = Split( englishPhrase ) 23 For k = 0 To UBound( words ) ' Check if first letter is a vowel If InStr( 1, "aeiou", _ LCase( Left( words( k ), 1 ) ) ) Then suffix = "y" Else suffix = "ay" End If 32 Piglatin.html Define Function procedure TranslateToPigLatin Split phrase into words Convert each word to pig Latin Function InStr searches a string for a substring. Function LCase returns a lowercase string.

26 Function Len returns the number of characters in a string.
' Convert the word to pig Latin words( k ) = Right( words( k ), _ Len( words( k ) ) - 1 ) & _ Left( words( k ), 1 ) & suffix Next 38 ' Return translated phrase, each word ' is separated by spaces TranslateToPigLatin = Join( words ) End Function 43 Sub cmdButton_OnClick() Dim phrase 46 phrase = Document.Forms( 0 ).txtInput.Value 48 Document.forms( 0 ).txtPigLatin.Value = _ TranslateToPigLatin( phrase ) End Sub > </script> </head> 55 <body> <form action = ""> Enter a sentence <input type = "text" name = "txtInput" size = "50" /> <p>Pig Latin <input type = "text" name = "txtPigLatin" size = "70" /> </p><p> <input type = "button" name = "cmdButton" value = "Translate" /></p> </form> </body> 66 </html> Function Right returns a string containing characters from the right side of a string argument. Function Len returns the number of characters in a string. Piglatin.html Function Left returns a string containing characters from the left side of a string argument. Return translated phrase using Join function Define an event procedure for cmdButton’s OnClick event.

27 Program Output

28 A simple Property Let procedure
1 Private mHour 2 3 Public Property Let Hour( hr ) If hr >= 0 And hr < 24 Then mHour = hr Else mHour = 0 End If 9 End Property A simple Property Let procedure Fig Simple Property Let procedure.

29 1 Public Property Get Hour() 2 Hour = mHour 3 End Property
A simple Property Get procedure Fig Simple Property Get procedure.

30 A simple Class definition
1 Class CTime1 Private mHour 3 Public Property Let Hour( hr ) If hr >= 0 And hr < 24 Then mHour = hr Else mHour = 0 End If End Property 11 Public Property Get Hour() Hour = mHour End Property 15 End Class A simple Class definition Fig Simple Class definition.

31 Define Property Let and Property Get procedures
1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!--Fig : classes.html --> 6 <!-- VBScript Class > 7 8 <html xmlns = " <head> <title>Using a VBScript Class</title> 11 <script type = "text/vbscript"> <!-- Option Explicit 15 Class Person Private name, yearsOld, ssn 18 Public Property Let FirstName( fn ) name = fn End Property 22 Public Property Get FirstName() FirstName = name End Property 26 Public Property Let Age( a ) yearsOld = a End Property 30 Public Property Get Age() Age = yearsOld End Property 34 Public Property Let SocialSecurityNumber( n ) Classes.html Define Class Person Define Property Let and Property Get procedures Define Property Let SocialSecurityNumber

32 Call function Validate Classes.html
36 If Validate( n ) Then ssn = n Else ssn = " " Call MsgBox( "Invalid Social Security Format" ) End If 43 End Property 45 Public Property Get SocialSecurityNumber() SocialSecurityNumber = ssn End Property 49 Private Function Validate( expression ) Dim regularExpression Set regularExpression = New RegExp 53 regularExpression.Pattern = "^\d{3}-\d{2}-\d{4}$" 55 If regularExpression.Test( expression ) Then Validate = True Else Validate = False End If 61 End Function 63 Public Function ToString() ToString = name & Space( 3 ) & age & Space( 3 ) _ & ssn End Function 68 End Class ' Person 70 Call function Validate Classes.html Define Function Validate Use regular expression to check format Define Function ToString.

33 Provide an event procedure for cmdButton’s OnClick event. Classes.html
Sub cmdButton_OnClick() Dim p ' Declare object reference Set p = New Person ' Instantiate Person object 74 With p FirstName = Document.Forms(0).txtBox1.Value Age = CInt( Document.Forms(0).txtBox2.Value ) SocialSecurityNumber =_ Document.Forms(0).txtBox3.Value Call MsgBox( .ToString() ) End With 82 End Sub > </script> </head> 87 <body> <form action = "">Enter first name <input type = "text" name = "txtBox1" size = "10" /> <p>Enter age <input type = "text" name = "txtBox2" size = "5" /></p> <p>Enter social security number <input type = "text" name = "txtBox3" size = "10" /> </p><p> <input type = "button" name = "cmdButton" value = "Enter" /></p> </form> </body> 100 </html> Provide an event procedure for cmdButton’s OnClick event. Classes.html Instantiate Person object. Use the With/End With statement to set several property values for p and call p’s ToString method.

34 Program Output

35 24.9 Operator Precedence Chart


Download ppt "Chapter 24 - VBScript Outline 24.1 Introduction 24.2 Operators"

Similar presentations


Ads by Google