Appendix C -- Answers to Quiz Questions
Appendix C
Answers to Quiz Questions
CONTENTS
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
Day 9
Day 10
Day 11
Day 12
Day 13
Day 14
Day 15
Day 16
Day 17
Day 18
Day 19
Day 20
Day 21
Day 1
The World Wide Web has become one of the most
popular services on the Internet, since it allows users to view
Web pages. Another very popular Internet service is FTP, since
that service allows Internet users to send and receive files on
the Internet. The ability of Web pages to allow you to use FTP
is a marriage of two of the most popular Internet services today.
Web pages are built using the Hypertext Markup
Language, or HTML. Other languages such as VBScript can be used
to make Web pages more powerful in ways that HTML cannot.
VBScript is not
a part of HTML, but HTML can include a tag
that will tell the browser to allow VBScript code to execute.
When VBScript code is placed within HTML code, a special "tag"
is used that tells HTML to allow another language to be run. The
Web browser is responsible for figuring out what that other language
is and passing control to it. Thus, when a VBScript is embedded
in an HTML document, the Web browser passes control to the VBScript
interpreter.
Day 2
The most significant capability a scripting language
affords a Web page is the ability of the user to interact with
the Web page. This means that the user can manipulate the various
controls, objects, and components on the Web page, and the code
that executes when those controls are manipulated accomplishes
the user's goals.
VBScript can work with and help integrate together
intrinsic HTML controls such as text box controls and buttons;
Java applets; ActiveX controls such as rotating labels, charts,
timers; calendars, status bars, and other specialized controls;
and embedded OLE objects, such as spreadsheets, graphics programs,
and word processors.
The VBScript code that comes along in a Web page
cannot directly access any files on the client's computer. This
prevents a Web page from making any modifications to sensitive
files on the user's computer. VBScript is also very safe in that
it doesn't give programmers the ability to create unrecoverable
crashes by virtue of its language syntax.
VBScript was derived from Microsoft Visual Basic.
A limited set of commands and functions were taken from Visual
Basic. This makes it easy for those who already know Visual Basic
to use VBScript-plus it leverages all the benefits of Visual Basic
to the Internet.
All you need is a browser that supports Visual
Basic Script, which typically includes the Visual Basic Script
run-time interpreter. Then, you are ready to use any page containing
Visual Basic Script. If you are going to develop Visual Basic
Script programs as well, you need a simple text editor to edit
HTML documents. Armed with these tools, you can write and use
Web pages that contain Visual Basic Script code.
Day 3
To state it in quite simplified terms, all you
need in each case is for the tool to have access to the VBScript
runtime interpreter, along with the ability of the software tool
to recognize when it needs to pass control to the runtime interpreter.
In addition to Internet Explorer, technologies
such as Next Software, Inc's WebObjects, Microsoft's Internet
Information Server, and Active VRML will take advantage of VBScript.
Many browsers do or will support VBScript, as well as many controls
designed for creating browsers or implementing that technology.
Because the Internet is changing so quickly, the list is growing
rapidly. In our opinion, most browsers will eventually support
VBScript, but at the time of this writing, the majority of Web
users were not yet on VBScript-aware browsers. Refer to the references
in Appendix B, "Information Resurces," to obtain up-to-date
information.
This code listing illustrates the "barebones"
structure of an HTML document that uses VBScript. Note that all
the HTML sections have starting and ending tags associated with
them:
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="VBScript">
<!--
VBScript code goes here
-->
</SCRIPT>
</BODY>
</HTML>
Day 4
The data type of VBScript variables is called
the variant. Using the variant, you can store data of all the
fundamental data subtypes listed in this lesson. The data subtypes
you can store include the integer, long, byte, Boolean, single,
double, date, and string data types.
Here's the answer:
Dim Name
Name = "Tim Koets"
Here's the answer:
If IsNumeric(Age) = False Then
MsgBox "The size must be a number.
Please enter it again."
End If
Day 5
The answer is shown in the following code segment.
The Web page, named 05quiz01.htm,
is contained on the CD-ROM that accompanies this book:
<HTML>
<TITLE>Chapter 5 Quiz #1</TITLE>
<H1><A HREF="http://www.mcp.com"><IMG ALIGN=BOTTOM
SRC="../shared/jpg/_
samsnet.jpg"
BORDER=2></A>
Converting Inches to Feet</H1>
<BODY>
<HR>
<CENTER>Feet <INPUT NAME="txtFeet">
<INPUT TYPE=BUTTON VALUE=" Convert " NAME="cmdConvert"></CENTER>
<HR>
<center>
from <em>Teach Yourself Visual Basic Script in 21 Days</em>
by
<A HREF="../shared/keith.htm">Keith Brophy</A>
and
<A HREF="../shared/tim.htm">Tim Koets</A><br>
Return to <a href="..\default.htm">Content Overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdConvert_OnClick()
Dim Inches, Feet
Feet = txtFeet.Value
Inches = Feet * 12
MsgBox "There are "
& Inches & " inches in " & Feet & "
feet."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
The answer is shown in the following code segment.
The Web page, named 05quiz02.htm,
is contained on the CD-ROM that accompanies this book:
<HTML>
<TITLE>Chapter 5 Quiz #2</TITLE>
<H1><A HREF="http://www.mcp.com"><IMG ALIGN=BOTTOM
SRC="../shared/jpg/_
samsnet.jpg"
BORDER=2></A>
The Ticket Stand</H1>
<BODY>
<HR>
<PRE>Name <INPUT
NAME="txtName"></PRE>
<PRE>Number of Tickets <INPUT
NAME="txtTickets"></PRE>
<PRE>Row Number (1 for front row) <INPUT NAME="txtRow"></PRE>
<CENTER><INPUT TYPE=BUTTON VALUE=" Get Cost "
NAME="cmdCost"></CENTER>
<HR>
<center>
from <em>Teach Yourself Visual Basic Script in 21 Days</em>
by
<A HREF="../shared/keith.htm">Keith Brophy</A>
and
<A HREF="../shared/tim.htm">Tim Koets</A><br>
Return to <a href="..\default.htm">Content Overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdCost_OnClick()
Dim Cost
Dim Ticket_Count
Ticket_Count = txtTickets.Value
Cost = 20.00 '
Base price
If txtRow.Value = 1 Then
Cost
= Cost + 4.00 '
Add $4 for front row
End If
Cost = Cost * Ticket_Count '
Get cost for all tickets
Cost = Cost + Cost * 0.03 '
Add 3% commission
Cost = Cost + Cost * 0.08 '
Add 8% sales tax
MsgBox "Your total cost
is " & Cost
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
a. 36
b. 2
c. -1
d. True
e. False
Day 6
If
Then-This
control structure is used to make decisions. If the condition
is satisfied, the code that follows the If
Then
statement is executed. A simple example of its use is
If Result = True Then
MsgBox "The
result is true."
End If
For
Next-This control structure is used to repeat
a block of code nestled between the For and Next statements based
on some condition.
A simple example of its use is
For x = 1 to 100
age(x) = 0
Next
Do
Loop While-This control
structure is used to repeat a block of code while the condition
is true.
An example of its use is
Do
age(x) = 0
x = x + 1
Loop While x < 100
Here's the code:
YearBorn = InputBox("What year were
you born in? ")
If YearBorn < 1890 Or YearBorn > 1990 Then
MsgBox "The year you have entered is invalid."
End If
Here's the code:
Proceed = False
Do
YearBorn
= InputBox("What year were you born in? ")
If YearBorn
< 1890 Or YearBorn > 1990 Then
MsgBox
"The year you have entered is invalid."
Else
Proceed
= True
End If
Loop Until Proceed = True
Day 7
A function, which is called differently from
a subroutine, returns a value to the code that calls it. Functions
must be set equal to a return value:
Answer = MyFunction(A,
B, C)
A subroutine, on the other hand, isn't
set to a variable. It can be called like
MySubroutine A, B, C
or
Call MySubroutine(A, B,
C)
It is impossible for a procedure to change the
value of a variable passed to it unless it gets its own copy.
Because the variable A
was passed to the function by value, the function GetHypotenuse
has its own copy, which is modified within the function. The original
variable, however, has exactly the same value it had before the
function was called.
The correct code listing is shown below. The
code was in error because the function CalculateCost
modified the two variables passed to it. Those variables were
passed by reference, not by value, which is illegal.
Sub cmdTest_OnClick()
Dim Base_Cost
Dim Total_Cost
Dim Tax
Tax = 5 ' Michigan
sales tax (5%)
Total_Cost = CalculateCost(Base_Cost,
Tax)
txtResult.Value = "The total cost
is $" & Total_Cost
End Sub
Function CalculateCost(ByVal Cost, ByVal Tax)
Tax = Tax
/ 100
Cost =
Cost + Tax * Cost
CalculateCost = Cost
End Function
The solution to the problem is to pass the two parameters
in by value so that the function has its own copy of the variables.
That way, it can modify the variables as it needs to. The new
code listing shows the necessary changes.
Day 8
The textarea control is different in many ways.
First, it gives you a two-dimensional area in which to place text.
Unlike the text control that only has a one-dimensional SIZE
attribute, the textarea control lets you adjust its dimensions
with the ROWS
and COLS
attributes. Finally, the textarea control has a starting and ending
tag, whereas the text control has a conventional input tag.
The following simple Web page, which is named
Quiz8-02.htm
on the CD-ROM, contains a textarea control and a button. The user
enters the products into the textarea control and clicks the button
as shown:
<HTML>
<HEAD>
<TITLE>Chapter 8 Quiz #1</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="..
/shared/jpg/samsnet.jpg" BORDER=2
HSPACE=20></A>
<EM>Chapter 8 Quiz Answer</EM></h1>
<HR>
<H2>Product Information</H2>
<FORM NAME="MyForm">
<P>Please enter all of the products
you would like information about
in the space below. Then, click on the
button and you will be
presented with a series of Web pages
detailing your selections.
<HR>
<PRE><TEXTAREA NAME="txaProducts"
COLS="60" ROWS="10"></TEXTAREA></PRE>
<P><INPUT TYPE="BUTTON"
NAME="cmdGetInfo"
VALUE=
"Get Product Information">
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript
in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith
Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim
Koets</A><br>
<br>
Return to <A href=Back08.htm>content
overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdGetInfo_OnClick()
MsgBox
"The products you're requesting information onÂ
will
be sent to you momentarily."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
All you need to do next is pass the products to the server and
get the information back to the user. You'll learn more about
how to do that on Day 19.
Day 9
The primary difference is that with a radio button,
the user can only select one of several choices. With a check
box, the user can select as many of the choices as he wants. Both
are useful depending on the circumstances. One important impact
this has on your VBScript code is that you can reference the checked
property of a check box. A radio button, on the other hand, must
be handled through code associated with the OnClick
event to know which button the user selected.
The text control: You use this control to accept
strings from the user in a simple box on the screen.
The button control: The button control provides
a 3-D button the user can click to perform some operation usually
designated on the caption of the button.
The textarea control: This is similar to a text
control, but the area the user can enter text into is bigger than
one line.
The radio button control: You commonly use radio
buttons to present a series of choices to the user where the user
can select only one choice.
The check box control: Check boxes enable the
user to select one or more choices from a list.
The password control: Use the password control
in place of the text control when you want the text entered in
the box to be masked so that the user can't see it.
The reset control: Use the reset control when
you want to provide a way for the user to reset a Web page to
when loaded. This is especially handy if the user is unable to
reload a page from the browser or other software he is using.
The submit control: This control is a button
that, when clicked, sends the contents of an HTML form to a Web
server. Use this when you are using CGI to communicate with a
server.
The combo control: The combo control is used
to present the user with a list of items, of which the user may
decide to select one or more items. Use this when you are unsure
of the type and number of items you will have, since the combo
control lets you select them dynamically.
The select control: The select control provides
the user with a list of items much like a menu. Use it to produce
a dynamic list of items, only allowing the user to select one
item.
The hidden control: The hidden control is like
a text control, except that it's invisible. Use it to store data
on a Web page instead of in a variable. This could be useful when
you wish to transfer data from one scripting language to another
or for an easy way to store data being transmitted across a Web
server.
The answer to this quiz question is provided
on the CD-ROM that accompanies this book. Figure C.1 shows the
file, which is named convert.htm.
Figure C.1 : A Web page that converts feet, inches, or yards to meters.
The following segment shows the source code for the Web page:
<HTML>
<HEAD>
<TITLE>Chapter 9 Quiz</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg"
BORDER=2 HSPACE=20></A>
<EM>Converting Feet, Inches or
Yards to Meters</EM></H1>
<HR>
<INPUT TYPE="TEXT" NAME="txtNumber"
SIZE="10">
<INPUT TYPE="RADIO" NAME="optUnits"
chECKED
OnClick="SetUnits('Feet')">
Feet
<INPUT TYPE="RADIO" NAME="optUnits"
OnClick="SetUnits('Inches')">
Inches
<INPUT TYPE="RADIO" NAME="optUnits"
OnClick="SetUnits('Yards')">
Yards
<P>
<INPUT TYPE="BUTTON" NAME="cmdResults"
VALUE="Calculate">
<INPUT NAME="txtResult"
SIZE="50">
<HR>
<center>
From <em>Teach Yourself VBScript
in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith
Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim
Koets</A><br>
<br>
Return to <A href=Back09.htm>content
overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Dim Units
Units = "Feet"
Sub SetUnits(NewUnits)
Units
= NewUnits
End Sub
Sub cmdResults_OnClick()
Dim
Number
Dim
Result
Number
= txtNumber.Value
If
Units = "Feet" Then
Result
= Number * 0.3048
ElseIf
Units = "Inches" Then
Result
= Number * 0.00254
ElseIf
Units = "Yards" Then
Result
= Number * 0.9144
End
If
txtResult.Value
= "There are " & Result & " meters in "
&_
Number
& " " & Units & "."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
The solution uses a script-level variable called Units
that tracks the user-selected units. Whenever the user clicks
a radio button, the subroutine SetUnits proceeds to change the
script-level Units variable. Then, when the user clicks Convert,
the script uses the appropriate multiplier in a conditional expression
to obtain the result. This example shows how to effectively use
intrinsic HTML controls, procedures, conditional structures, and
script-level variables to accomplish the task.
Day 10
The following is the code:
<SCRIPT LANGUAGE="VBSCRIPT">
<!--
sub lblTester_Click
lblTester.Caption =
"Kenny Jasper"
end sub
-->
</SCRIPT>
The following is the code:
If (lblTester.Angle >= 20)and (lblTester.Angle
<= 40) then
lblTester.Caption
= "Brooke"
else
lblTester.Angle
= lblTester.Angle + 10
end if
Day 11
The following is the code:
Sub MyTimer_Time
' Check to see what the current interval is
if MyTimer.Interval = 2000 then
MyTimer.Interval = 1000
elseif MyTimer.Interval = 1000 then
MyTimer.Interval = 500
else
MyTimer.Enabled = 0
end if
end sub
The following is the code:
MyItem.date = "7/6/62"
By setting the suppression date back in time, you
ensure that the code will consider that the suppression date has
already been reached, and the graphic will not display.
Day 12
The following code segment shows the answer.
Notice that because the Java applet resides at a different location
from the current page, you must provide CODEBASE.
CODETYPE
and the associated apology message are optional.
<OBJECT
ID="jvaGifts"
CLASSID="java:Birthday.Logs"
CODETYPE="application/java-vm"
CODEBASE="http://www.mcp.com/javastuff/"
HEIGHT=100
WIDTH=100
>
<PARAM NAME="MaxSpend" VALUE="200">
A java applet is used on this page but your browser does not support
that.
</OBJECT>
jvaGifts.MaxSpend = "300"
Day 13
Indentation is used to show the hierarchy of
the code under the conditional checks. Good variable names indicate
the type and the purpose of each variable. The combination of
these changes results in a much more readable program:
vAge=txtage.value
If IsNumeric(vAge) then
if vAge < intGENERATION_X_AGE Then
msgbox "Dude, prepare
to surf!"
Else
msgbox "Welcome
- Prepare to enter our Web page"
End If
Elseif vAge = "unknown" Then
msgbox "you must supply an age to
proceed"
Else
msgbox "please supply a valid age"
End If
The following code segment shows the two event
scripts merged into one consolidated script:
<script Language="VBScript">
sub lblFeedback_click
msgbox "Feedback label has been clicked"
end sub
Sub cmdCalculate_OnClick
msgbox "Button has been clicked"
End sub
</script>
Day 14
The solution involves prompting the user with
a Yes/No message box and, if the user responds yes, generating
the <H2>Extra Hints<H2>
tag with document.write.
Here's the solution:
<HTML>
<HEAD>
<TITLE>Brophy & Koets' Teach Yourself VBScript - Quiz
1 Solution</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=VBS>
<!-
dim rc '
Stores return code value
rc = msgbox("Do you want extra hints
to be displayed on this page?", _
vbYesNo
+ vbQuestion,"Setting up the page")
if rc = vbYes then
' Show Big Spender
Page
Spender = vbTrue
document.write
"<H2>Extra Hints</H2>"
' ... more document.write
statements to display hints could go here
end if
-->
</SCRIPT>
<! Regular HTML statements that are always carried out could
follow... -->
<H3>Welcome to the Question Page</H3>
<p>Q1. What eats lots of oranges and has 80,000 legs?
<p>Q2. Why is the grass green?
<BR>
<BR>
<p>A1. The field in the Boston Marathon
<p>A2. If it was white you couldn't tell if a polar bear
was in your lawn!
</BODY>
</HTML>
Since the page is generated sequentially, the
script will be carried out in the order in which it is encountered.
Moving the script between the question and answer statements causes
it to be carried out, and Extra
Hints to be generated on the page, in
that sequence. When the user responds affirmatively, he will see
the Extra Hints
text right after the question area on the Web page. Here's the
solution:
<HTML>
<HEAD>
<TITLE>Brophy & Koets' Teach Yourself VBScript - Quiz
2 Solution</TITLE>
</HEAD>
<BODY>
<! Regular HTML statements that are always carried out could
follow... -->
<H3>Welcome to the Question Page</H3>
<p>Q1. What eats lots of oranges and has 80,000 legs?
<p>Q2. Why is the grass green?
<BR>
<SCRIPT LANGUAGE=VBS>
<!-
dim rc '
Stores return code value
rc = msgbox("Do you want extra hints
to be displayed on this page?", _
vbYesNo
+ vbQuestion,"Setting up the page")
if rc = vbYes then
' Show Big Spender
Page
Spender = vbTrue
document.write
"<H2>Extra Hints</H2>"
document.write
"<p>Q1. Think about 26.2 miles"
document.write
"<p>Q1. It involves an animal"
' ... more document.write
statements to display hints could go here
end if
-->
</SCRIPT>
<BR>
<p>A1. The field in the Boston Marathon
<p>A2. If it was white you couldn't tell if a polar bear
was in your lawn!
</BODY>
</HTML>
Use the vbDefaultButton2
intrinsic constant to cause the second button to be highlighted:
rc = MsgBox ("Do you want poor Fred
to bike 68 miles by himself?", _
vbYesNo + vbDefaultButton2,
"Biking Question")
Day 15
The following is the code:
if StrComp(txtDepartment.Value, "Accounting",
1) = 0 then
' Accounting department has been entered
else
' Accounting department was not entered
end if
Note that the non-0 third parameter to StrComp instructs
the function to carry out a match that is not case sensitive.
Many variations of this code would carry out
the algorithm. The key is to loop through the string, using InStr
to check for an occurrence of Brown
and advancing the start position for InStr
each time through the loop. The following is the solution:
dim blnDone ' indicates when
the string count is complete
dim intCount ' counter of
how many times Brown is detected in string
dim intFoundPosition ' the position where Brown is
found in string
dim intCurrentPosition ' the next position to search
for Brown in string
' Initialize variables to start looking at string
blnDone = False
intCurrentPosition = 1
' Continue to advance through string until done
' condition no more Brown's are found
do while not blnDone
' Find where the next Brown is
intFoundPosition = InStr(intCurrentPosition, _
s_strBorrower, "Brown")
' See if Brown was found
if intFoundPosition = 0 then
' No more Brown's
are in string so the search is done
blnDone = vbTrue
else
' Brown was found,
increment count and prepare
' to search again
after this one
intCount = intCount
+ 1
intCurrentPosition
= intFoundPosition + len("Brown")
end if
loop
msgbox "Brown borrowed the book " & intCount &
" times. "
Day 16
Here's the answer:
Dim dtmNow
' Get current time
dtmNow = Now()
if s_dtmEvent < dtmNow then
msgbox "Registration deadline is
passed."
End if
Int
always truncates left on the number line. Fix
truncates toward the number line. Cint
rounds to the nearest even number. Here are the numeric results:
Line 1: -6
Line 2: -5
Line 3: -6
Here's the answer:
dim intCounter, intLeftOverPlayers
for intCounter = 1 to 1000
intLeftOverPlayers = _TotalPlayers
Mod 9
next
msgbox intLeftOverPlayers
Day 17
One sequence of trace messages you can use is
to insert a trace in every condition:
If a > 3 then
b = c + 1
MsgBox "After b= c + 1", vbOKOnly
, "Debug-Time Trace"
else
if a = 1 then
b = c + 4
MsgBox "After b= c +
4", vbOKOnly , "Debug-Time Trace"
else
if < -2 then
b = c +
7
MsgBox "After
b= c + 7" vbOKOnly , "Debug-Time Trace"
end if
end if
end if
The key is to inspect the values of the variables
and the variable type of the
variables:
dim strVarP, strVarQ, strVarR
' Retrieve the subtype and value of each
of the variables and then display_ it.
' vbCrLf is used to separate lines in the
output message.
strVarP = "(P) Subtype = " & VarType(P) & " Value
= " & P
strVarQ = "(Q) Subtype = " & VarType(Q) & " Value
= " & Q
strVarR = "(R) Subtype = " & VarType(R ) & " Value
= " & R
MsgBox strVarP & vbCrLf & strVarQ & vbCrLf
& strVarR, vbOKOnly, "Debug Variables"
r = q / p
This code will print the value and subtype of each
variable. This trace code must be inserted before the problem
statement. If you inserted it after the problem statement, the
runtime error would occur first and your trace statement would
never be reached (unless you used On Error
Resume Next).
The statements will print the variable's current value as well
as the subtype, but nothing will be displayed when printing the
value of an empty or a null variable. In some cases, the subtype
of the variable may be empty or null. If so, the value returned
by the VarType function will
provide you with this information. (See Day 4 for a full discussion
of VarType capabilities.)
For other subtypes, the standard subtype and value of the variable
are displayed.
With this information on each variable involved in the equation,
you should be able to pinpoint the cause of any type of error
that occurs. Inspection of this information would show that one
of the variables being used in the division has an underlying
subtype of double (VarType
= 5) and the other has an underlying subtype of string data (VarType
= 8). The remaining variable has a subtype of empty (VarType
= 0), but that could be correct since that is the result variable
and perhaps no values have yet been assigned to it. The fact that
a string is being used in the division, however, should be enough
to tell you there is an errant code assignment somewhere, and
lead you to other areas of the code to find the culprit. One potential
cause of such a problem could be code like the following:
dim p, q, r
p = 1.3
q = "a"
r = q / p
This type of error is obvious if the statements are
clustered together, but if the assignments are not in the vicinity
of the division statement, then analyzing variables is usually
a necessary first step to knowing where to look for the cause
of the problem.
Another way to carry out the same type of analysis would be to
use the VarAnalyze procedure.
This procedure provides the same capabilities as the code snippet
above, but the procedure itself generates information each time
it is called. The procedure also provides a verbal description
of the VarType (such as empty)
rather than just the numeric value. If the VarAnalyze
procedure were used, the trace would appear like this:
' Retrieve the subtype and value of each
of the variables and then display_ it.
' vbCrLf is used
to separate lines in the output message.
VarAnalyze ("Contents of p prior to the division:
", p)
VarAnalyze ("Contents of q prior to the division:
", q)
VarAnalyze ("Contents of r prior to the division:
", r)
r = q / p
Day 18
The name is formed by joining the anchor name
with the OnClick
event:
sub lnkBooks_OnClick
. . .
end sub
The name is formed by joining the anchor name
with the OnClick
event:
sub lnkBooks_MouseMove (s, b, x, y)
. . .
end sub
You can redirect the link with the location object's
HREF
property. Just assign the Uniform Resource Locator address of
the page you wish to visit to location.href.
sub lnkBooks_Click
location.href = "http://www.microsoft.com/vbscript"
end sub
Day 19
You must supply a return value with the form
OnSubmit method, by making the one-line modification as shown
in the following code:
' Make sure the user
age is 18 or over
if document.frmApply.txtAge.value < 18 then
MsgBox "Sorry youngster,
you're not old enough to apply!", _
vbOKOnly,"Can't
take app"
frmApply_OnSubmit=False
else
document.frmApply.Submit
MsgBox "Application
processed", vbOKOnly, "Confirmation"
frmApply_OnSubmit=True
end if
end sub
-->
The following is the modified code:
<!--
Function frmApply_OnSubmit
' Process the application by submitting to server if data is valid
' Make sure the user age is 18 or over
if document.frmApply.txtAge.value < 18 then
MsgBox "Sorry youngster,
you're not old enough to apply!", _
vbOKOnly,"Can't
take the application"
elseif len(document.frmName) = 0 then
frmApply_OnSubmit=False
MsgBox "Sorry,
we can't take an application from just nobody!",
vbOKOnly,"Can't
take app"
frmApply_ONSubmit=false
else
document.frmApply.Submit
MsgBox "Application
processed", vbOKOnly, "Confirmation"
frmApply_OnSubmit=True
end if
end function
-->
</script>
Day 20
VBScript cannot make use of DLLs, so the option
of using the DLL in the code is out. You could rewrite the DLL
code into an ActiveX control if you have a language such as C++
that can generate these controls. Then you could make use of that
from within your VBScript for the same purpose, or you could rewrite
the DLL code into script subroutines, which are themselves a part
of the script. This approach is likely to be too cumbersome if
there is too much code in the DLL. Most likely the ActiveX control
strategy is the quickest path to reach the same level of function
in the script as the original program.
(a)VBScript does not support constants, so TAX_RATE
and ITEM_PRICE
must be declared as variables. (b)VBScript only supports the variant
variable type, so the variable declarations for intUnits
and intCost
must be modified accordingly. (c)The value assigned to intCost
will no longer be an integer under VBScript. Under VBScript the
variable will have to be declared to be of type variant because
that's the only type supported. Because a variant can assume any
type, the variable will assume a single type (decimal point number)
after this assignment.
The following is the code, modified to work for
VBScript:
dim TAX_RATE
dim ITEM_PRICE
Dim intUnits as Integer
Dim intCost as Integer
' Set up constant values
TAX_RATE = 0.04
ITEM_PRICE = 25
' Calculate the total cost
intUnits = InputBox("How many units do you wish to purchase?")
intCost = (ITEM_PRICE * intUnits) * TAX_RATE
' Store cost in its integer representation
intCost = cInt(intCost)
Day 21
Any language features that relate to external
communication and control fall into this category. They include
normal file input and output, clipboard support, printer object
support, dynamic link library calls, and calls to system application
programming interfaces.
Commercial controls with the highest level of
certification have been verified by an independent certification
laboratory. This does not mean they are bug free, but it does
imply an overall level of general soundness. If a control is certified,
it carries with it a digital signature so that the creator of
the control is clearly and uniquely identified. This provides
a trail of accountability if any problems occur.
Wyszukiwarka
Podobne podstrony:
appcappCappcappcappcappcappcappcappc (9)appcappcappcappc (6)appcappcappC0024 appcappcwięcej podobnych podstron