085 088




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




You can use a constant anywhere you can use any Visual Basic expression, and you usually use them for numeric or string values that you want to use many places in a program. That way, when you want to modify the value of the constant, you only have to change it in its declaration, not in many places around the program. Also, constants don’t change their values, which can make them more useful than variables in certain circumstances.


TIP:  You can’t use variables, user-defined functions, or intrinsic Visual Basic functions in expressions assigned to constants.

Here’s an example showing how to declare and use a constant:



Private Sub Command1_Click()
Const Pi = 3.14159
Dim Radius, Area
Radius = 1#
Area = Pi * Radius * Radius
MsgBox ("Area = " & Str(Area))
End Sub


Declaring Variables
Before using variables, you have to set aside memory space for them—after all, that’s what they are, locations in memory. Usually, you use the Dim statement to declare variables, although you can also use the Private (declare a private variable), Public (declare a global variable), Static (declare a variable that holds its value between procedure calls), ReDim (redimension a dynamic array), or Type (declare a user-defined type) keywords to declare variables, as we’ll see in the tasks covered in this chapter.
The Dim Statement
Here’s how you use the Dim statement:


Dim [WithEvents] varname[([subscripts])] [As [New] type] [, [WithEvents]
varname [([subscripts])] [As [New] type]] . . .


The WithEvents keyword is valid only in class modules. This keyword specifies that varname is an object variable used to respond to events triggered by an ActiveX object. The varname identifier is the name of the variable you are declaring. You use subscripts if you’re declaring an array.
You set up the subscripts argument this way:


[lower To] upper [, [lower To] upper]



TIP:  In Visual Basic, you may declare up to 60 dimensions for an array.

The New keyword enables creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it. This means you don’t have to use the Set statement to assign the object reference. Here’s an example:


Dim DataSheet As New Worksheet


The type argument specifies the data type of the variable, which may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. If you don’t specify a type, the default is Variant, which means the variable can act as any type.

TIP:  By default in Visual Basic, numeric variables are initialized to 0, variable-length strings are initialized to a zero-length string (“”), and fixed-length strings are filled with zeros. Variant variables are initialized to Empty.

Here’s an example of declaring variables using Dim:


Dim EmployeeID As Integer
Dim EmployeeName As String
Dim EmployeeAddress As String


Implicit Declarations And Option Explicit
Following the traditions of earlier versions of Basic, you don’t actually need to declare a variable at all to use it—just using it in code declares it as a variant if it’s not been declared. It’s better to require all variables to be explicitly declared, however, because misspelling a variable name can declare a new variable and cause problems, as we saw in this code from Chapter 1, where we think we’re toggling a Boolean variable named xNow but are placing the result in a new and misspelled variable named xNoww:


Private Sub Command_Click(Index As Integer)
If xNow Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If

xNoww = Not xNow

End Sub


To force variable declarations to be explicit (that is, to insist that each variable be declared), add the Option Explicit statement at the module or form level to the (General) declarations object.
Selecting Variable Types
It’s time to create a new variable—but what type should you use? For that matter, exactly what type of variable types are there and what do they do? Even if you remember what types there are, you probably won’t remember the range of possible values that variable type allows.




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:
085 088
085 088
088 COURT
086 088
2 4 Układy hybrydoweid 088
v 03 088
088 089
088 89
083 085
v 04 088
085 Szwedka

więcej podobnych podstron