096 101




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




The Public keyword makes a procedure accessible to all other procedures in all modules and forms. The Private keyword makes a procedure accessible only to other procedures in the module or form in which it is declared. The Friend keyword is used only in class modules and specifies that the procedure is visible throughout the project, but not visible to a controller of an instance of an object. The Static keyword specifies that the procedure’s local variables should be preserved between calls. The name identifier is the name of the procedure. The arglist identifier is a list of variables representing arguments that are passed to the procedure when it is called. You separate multiple variables with commas. The statements identifier is the group of statements to be executed within the procedure.
The arglist identifier has this following syntax:


[Optional] [ByVal | ByRef] [ParamArray] varname [()] [As type]
[= defaultvalue]


In arglist, Optional means that an argument is not required; ByVal means that the argument is passed by value; ByRef means that the argument is passed by reference (ByRef is the default in Visual Basic); ParamArray is used as the last argument in arglist to indicate that the final argument is an array of Variant elements; varname is the name of the variable passed as an argument; type is the data type of the argument; and defaultvalue is any constant or constant expression, which is used as the argument’s default value if you’ve used the Optional keyword. The type identifier is the data type returned by the function. The Exit Function keywords cause an immediate exit from a Function procedure.
You call a Function procedure using the function name, followed by the argument list in parentheses. You return a value from a function by assigning the value you want to return to the function’s name like this: name = expression. Finally, End Function ends the procedure definition.
Here’s an example showing how to use a function:


Private Sub Command1_Click()
Dim intResult As Integer
intResult = Add1(5)
MsgBox ("Result = " & Str$(intResult))
End Sub

Function Add1(intAdd1ToMe As Integer) As Integer
Add1 = intAdd1ToMe + 1
End Function


Preserving Variables’ Values Between Calls To Their Procedures
You’ve written a function named Counter() to keep track of the number of times the user clicks a particular button. Each time the user clicks the button, you call the Counter() function to increment the count of button clicks, and then display the result in a message box. But the counter never seems to be incremented; instead it always returns 1. Why?
Let’s look at the code:


Private Sub Command1_Click()
Dim intResult As Integer
intResult = Counter()
MsgBox ("Result = " & Str$(intResult))
End Sub

Function Counter() As Integer
Dim intCountValue As Integer
intCountValue = intCountValue + 1
Counter = intCountValue
End Function


The problem here is that the counter variable, intCountValue, in the Counter() function is reinitialized each time the Counter() function is called (because a new copy of all the variables local to procedures is allocated each time you call that procedure).
The solution is to declare intCountValue as static. This means it will retain its value between calls to the Counter() function. Here’s the working code:


Private Sub Command1_Click()
Dim intResult As Integer
intResult = Counter()
MsgBox ("Result = " & Str$(intResult))
End Sub

Function Counter() As Integer
Static intCountValue As Integer
intCountValue = intCountValue + 1
Counter = intCountValue
End Function


In fact, you could declare the whole function static, which means that all the variables in it will be static. That looks like this:



Private Sub Command1_Click()
Dim intResult As Integer
intResult = Counter()
MsgBox ("Result = " & Str$(intResult))
End Sub

Static Function Counter() As Integer
Dim intCountValue As Integer
intCountValue = intCountValue + 1
Counter = intCountValue
End Function


Besides declaring variables with Static, you can also use it as a keyword when declaring functions or subroutines.
Handling Strings
You’ve decided to lead the way into the future by letting your users type in English sentences as commands to your program. Unfortunately, this means that you have to parse (that is, break down to individual words) what they type. So what was that string function that lets you break a string into smaller strings again? We’ll get an overview of string handling in this topic.
Two Kinds Of Strings
There are two kinds of strings: variable-length and fixed-length strings. You declare a variable-length string this way:



Dim strVariableString As String


A variable-length string can contain up to approximately 2 billion characters, and it can grow or shrink to match the data you place in it.

You declare a fixed-length string this way, with an asterisk character (*) followed by the string’s length:


Dim strFixedString As String * 20


Here, we give our fixed-length string 20 characters. A fixed-length string can contain 1 to approximately 64K characters.

The String-Handling Functions
There are quite a number of string-handling functions in Visual Basic. For example, you use Left(), Mid(), and Right() to divide a string into substrings, you find the length of a string with Len(), and so on.
For reference, the Visual Basic string-handling functions appear in Table 3.5.
Table 3.5 String-handling functions.

To Do This
Use This

Compare two strings
StrComp

Convert strings
StrConv

Convert to lowercase or uppercase
Format, LCase, UCase

Create string of repeating character
Space, String

Find length of a string
Len

Format a string
Format

Justify a string
LSet, RSet

Manipulate strings
InStr, Left, LTrim, Mid, Right, RTrim, Trim

Set string comparison rules
Option Compare

Work with ASCII and ANSI values
Asc, Chr





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:
t informatyk12[01] 02 101
odp cz 112[01] 0X 101
27 (101)

więcej podobnych podstron