![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Heed my advice: Turn on the Require Variable Declaration option, and leave it on. In fact, I feel so strongly about this point that Im going to elevate it to the exalted status of a rule: Always require variable declarations. When you are typing a variable declaration and enter the As keyword followed by a space, the Visual Basic editor automatically displays a list of all the data and object types that you could use for the variable. If you start typing a type name, the list scrolls to the corresponding entry. This is shown in Figure 4.1, although the list contains many more types than the ones weve learned about here. As we will cover in a later chapter, Visual Basic variables can refer to objects as well as to data, hence the extra types on this list. You can turn this auto-list feature on and off by selecting Options from the Tools menu, displaying the Editor tab, and clicking on the Auto List Members option. String VariablesThe second major category of variables is strings. This is simply computer talk for text, but I am guessing this use of string comes from the fact that text is nothing more than a string of characters. A string variable can hold a single character or thousands of characters, or it can be empty. Depending on the needs of your program, you can choose from Visual Basics two types of string variables. A fixed-length string has a maximum capacity that you set when you declare it. A fixed-length string can hold any number of characters up to this maximum lengthbut not over it. In contrast, a variable-length string is akin to a rubber bag that automatically adjusts its size to hold however many characters you place in it. For both types of string variables, the maximum capacity is some 2 billion charactersthats right, billion.
Fixed-length string variables must be declared (another argument for using Option Explicit). The declaration takes the following form: Dim LastName As String * 5 The number in the declaration (in this example, 5) specifies the string length. Remember, this is the maximum number of characters you can store in the string. If you try to put more characters in the variable, they will be lost. For example, if you execute the statement LastName = Aitken only the first five characters will be stored; the n will be lost. You can, however, put fewer than the maximum number of characters in a fixed-length string. As for variable-length strings, you can create one by using its type declaration character $, but the Dim statement remains the preferred method of declaring it: Dim FirstName As String Now you have a string variable named FirstName, into which you can stuff as large a string as you likeat least up to 2 billion characters, which I dont consider too limiting. Which type of string variable should you use? For the most part, the decision is based on your programs data. True, you can manipulate fixed-length strings a bit more quickly. With todays lightning-fast processors, however, it wont make any noticeable difference unless your program performs an enormous amount of text processing. If you are absolutely sure your string data wont ever exceed a certain length, a fixedstring is probably best. Otherwise, use a variable-length string. Other Variable TypesFour more Visual Basic data types are used in special situations: Boolean, Date, Variant, and Object. The Boolean type holds information that takes the form of yes/no or on/off. In other words, a Boolean variable can hold only two possible values, referred to by the Basic keywords True and False. Youll see that Boolean variables are closely related to logical expressions, which Ill cover later in the chapter. A Boolean variable requires two bytes of storage, the same as type Integer. The Date type holds date and time information. It is a floating point type, with the whole-number portion representing the date and the fractional portion representing the time. The date is encoded as the number of days since December 30, 1899, with negative numbers representing earlier dates. The time is encoded as a fraction of the 24-hour day, with 0.0 representing midnight, 0.25 representing 6:00 a.m., 0.5 representing noon, and so on. Variant is Visual Basics catchall data type. It can be assigned any of Visual Basics data types except fixed-length strings. The main use of Variant variables is to simplify dealing with numerical data that sometimes needs to be treated as a string. Later in the book, Ill cover some other specialized uses of type Variant. Finally, the Object data type is used to hold a reference to an object. Youll learn more about using this data type in later chapters. None of these four data types has a type declaration character associated with it. The Date, Boolean, and Object types must be declared in a Dim statement: Dim Answer As Boolean, OrderDate As Date, MyThing As Object The Variant type can also be declared in a Dim statement. If you are not requiring variable declarations and you use a variable name without a type declaration character, Visual Basic automatically makes it a type Variant. Remember, however, that using variables without explicitly declaring the type is to be avoided. User-Defined Data TypesOne of Basics handiest features is the ability to create user-defined data types. A user-defined type (also called a structure) is a compound data type containing two or more other data types. You can define exactly what goes into a structure, designing it around the exact needs of your program. You use the Type...End Type statement to define a structure. Heres how it looks: Type StructureName element1 As Type element2 As Type ... End Type If you were writing a program to maintain an inventory list, for example, you could define a structure as follows to hold information about each item: Type StockItem Name As String * 25 PartNumber As String * 12 Cost As Currency NumberOnHand As Integer End Type The individual elements of a structure can be any of Basics fundamental data types except variable-length strings. You can even use one structure type as an element in another structure type. Furthermore, while I havent introduced arrays yet (that comes soon), I will mention that an array can be an element of a structureand you can also create arrays of structures. When you define a structure with the Type...End Type statement, thats all you are doingdefining it. The Type...End Type statement does not create any actual variables of the defined type. You must use the Dim statement to create instances of the structureactual variables with names and with memory space allocated to them. Using our previous example, once we have defined the StockItem type, we can then create an instance of the type as follows: Dim NewPart As StockItem Now that you have a structure of type StockItem named NewPart to work with, how do you access its various elements? You use the variable name followed by a period and the element name, as shown here: NewPart.Name = cam tensioning spring NewPart.PartNumber = L-101-6J NewPart.Cost = 99.76 NewPart.NumberOnHand = 8
|
![]() |
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. Read EarthWeb's privacy statement. |