105 109




Visual Basic 6 Black Book:The Visual Basic Language
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Here’s an example using Select Case. In this example, we read a positive value from the user and test it, responding according to its value. Note that we also use the Select Case Is keyword (not the same as the Is operator) to check if the value we read in is greater than a certain value, and Case Else to handle values we don’t explicitly provide code for. Here’s the example:


Dim intInput
intInput = -1

While intInput < 0
intInput = InputBox("Enter a positive number")
Wend

Const intMax = 100

Select Case intInput
Case 1:
MsgBox ("Thank you.")
Case 2:
MsgBox ("That's fine.")
Case 3:
MsgBox ("Your input is getting pretty big now...")
Case 4 To 10:
MsgBox ("You are approaching the maximum!")
Case Is > intMax:
MsgBox ("Too big, sorry.")
Case Else:
MsgBox ("Please try again.")
End Select


Making Selections With Switch() And Choose()
For some reason, few books on Visual Basic cover the Switch() and Choose() functions. They certainly have their uses, however, and we’ll take a look at them here.
The Switch() Function
The Switch() function evaluates a list of expressions and returns a Variant value or an expression associated with the first expression in the list that is true. Here’s the syntax:


Switch (expr-1, value-1[, expr-2, value-2 ... [, expr-n, value-n]])


In this case, expr-1 is the first expression to evaluate; if true, Switch() returns value-1. If expr-1 is not True but expr-2 is, Switch() returns value-2 and so on.
Here’s an example showing how to use Switch(). In this case, we ask the user to enter a number and use Switch() to calculate the absolute value of that value (having temporarily forgotten how to use the built-in Visual Basic absolute value function, Abs()):


Dim intValue

intValue = InputBox("Enter a number")

intAbsValue = Switch(intValue < 0, -1 * intValue, intValue >= 0, intValue)

MsgBox "Absolute value = " & Str(intAbsValue)


The Choose() Function
You use the Choose() function to return one of a number of choices based on an index. Here’s the syntax:


Choose (index, choice-1 [, choice-2, ... [, choice-n]])


If the index value is 1, the first choice is returned, if index equals 2, the second choice is returned, and so on.

Here’s an example using Choose(). In this case, we have three employees—Bob, Denise, and Ted—with employee IDs 1, 2, and 3. This code snippet accepts an ID value from the user and uses Choose() to display the corresponding employee name:


Dim intID
intID = -1

While intID < 1 Or intID > 3
intID = InputBox("Enter employee's ID")
Wend

MsgBox "Employee name = " & Choose(intID, "Bob", "Denise", "Ted")


Looping
Many programmers have a love/hate relationship with looping, based primarily on syntax. Programmers often have to switch back and forth these days between languages, and can find themselves writing, for example, a C++ loop in the middle of a Visual Basic program and being taken by surprise when the compiler objects.

To make it easier, we’ll include examples here of all the Visual Basic loops, starting with the Do loop.
The Do Loop
The Do loop has two versions; you can either evaluate a condition at the beginning


Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]
Loop


or at the end:



Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]


Here’s an example where we read from a file, looping until we reach the end of the file, which we check with the end-of-file function, EOF():


Do Until EOF(1)
Line Input #1, Data$
Form1.TextBox1.Text = Form1.TextBox1.Text + Data$
Loop



TIP:  Note that the second form of the Do loop ensures that the body of the loop is executed at least once. On the other hand, you sometimes want to make sure the loop doesn’t run even once if the condition is not met. For example, when reading from a file, you shouldn’t read from a file before checking for the end of file in case the file is empty.

The For Loop
The Do loop doesn’t need a loop index, but the For loop does. Here’s the syntax for the For loop:


For index = start To end [Step step]
[statements]
[Exit For]
[statements]
Next [index]


Here’s how to put it to work:



Dim intLoopIndex, Total
Total = 0
For intLoopIndex = 1 To 10
Total = Total + 1
Next intLoopIndex



TIP:  Although it’s been common practice to use a loop index after a loop completes (to see how many loop iterations were executed), that practice is now discouraged by people who make it their business to write about good and bad programming practices.

The For Each Loop
You use the For Each loop to loop over elements in an array or collection. Here’s its syntax:


For Each element In group
[statements]
[Exit For][statements]
Next [element]


You can get a look at this loop in action with an example like this one, where we display all the elements of an array in message boxes:



Dim IDArray(1 To 3)
IDArray(1) = 1
IDArray(2) = 2
IDArray(3) = 3

For Each ArrayItem In IDArray
MsgBox (Str(ArrayItem))
Next ArrayItem


The While Loop
You use a While loop if you if you want to stop looping when a condition is no longer true. Here’s the While loop’s syntax:


While condition
[statements]
Wend


And here’s an example putting While to work:


Dim intInput
intInput = -1

While intInput < 0
intInput = InputBox("Enter a positive number")
Wend



TIP:  Many Visual Basic functions, like EOF(), are explicitly constructed to return values of True or False so that you can use them to control loops such as Do and While loops.





Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.



Wyszukiwarka

Podobne podstrony:
IV CSK 109 09 1
105 1
105 04 (10)
IR(109)$07 pl
105 108 (2)
rozdzial (105)
108 109
v 02 105

więcej podobnych podstron