![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
The key Visual Basic techniques used to break AppointmentBook into manageable pieces are outlined in the following list. The rest of this chapter describes them in detail.
Using Bit MasksComputers store any data item as a series of bits, each of which can have the value zero or one. For example, in Visual Basic an integer is represented by a series of 16 bits; a long integer is represented by 32 bits. A program can use the bits in one of these data types to record Boolean or on/off features. A feature is present, true, or on if the corresponding bit in the integer is a one. It is absent, false, or off if the bit is a zero. The series of bits 00...011 would indicate that only the last two features were present. A program can use an integer if it will need no more than 16 features and a long integer if it needs up to 32 features. A bit mask is a value that represents a specific collection of values. Often a mask represents a single feature. For instance, the value 00...001 represents a value with only the last feature present. The following sections explain how to use bit masks and how they are used in AppointmentBook. If you are already familiar with bit masks, you may want to skip to the section titled Bit Masks in AppointmentBook. Defining Bit MasksThe following code defines bit mask values that describe specific vehicle features:
Public Const VEH_4WD = &H1 Four-wheel drive.
Public Const VEH_ABS = &H2 Anti-lock brakes.
Public Const VEH_Airbag1 = &H4 Driver-side airbag.
Public Const VEH_Airbag2 = &H8 Passenger airbag.
Public Const VEH_Automatic = &H10 Automatic transmission.
Public Const VEH_PowerSteering = &H20 Power steering.
Public Const VEH_PowerBrakes = &H40 Power brakes.
Public Const VEH_PowerWindows = &H80 Power windows.
Public Const VEH_RemoteKeyless = &H100 Remote keyless entry.
:
This code illustrates several important points. First, each masks name begins with VEH_. This makes it easy to remember that the values represent vehicle features. Defining the masks using names such as ABS and 4WD without the VEH_ prefix would make other parts of the code that use the masks harder to understand. The definition of each mask includes a comment that indicates exactly what the mask means. The mask name itself should be as meaningful as possible, but it is still a good idea to place comments near the definitions. Later, if it is unclear what VEH_DOHC means, a quick search for the string VEH_DOHC = will locate the comment. In this example, it would also be best to change the name to something less cryptic, such as VEH_DualOverheadCam. Finally, the values are defined using hexadecimal. This makes it easy to keep track of the next integer value that has only a single bit set. In base 10, the numbers with a single bit set are 1, 2, 4, 8, 16, 32, 64, .... In hexadecimal, the same numbers are 1, 2, 4, 8, 10, 20, 40, .... The sequence consists of the digits 1, 2, 4, and 8 followed by different numbers of zeros. This simple pattern makes it easy to generate as many hexadecimal values as necessary. Alternatively, mask values can be specified in terms of the preceding values. A mask value with a single bit set multiplied by two gives the next value with a single bit set. The following code defines the first few vehicle codes using this method:
Public Const VEH_4WD = 1 Four-wheel drive.
Public Const VEH_ABS = VEH_4WD * 2 Anti-lock brakes.
Public Const VEH_Airbag1 = VEH_ABS * 2 Driver-side airbag.
Public Const VEH_Airbag2 = VEH_Airbag2 * 2 Passenger airbag.
:
This method has the advantage that it requires few changes to move items around in the list. The following code shows the same list with the new value VEH_Automatic inserted in the middle. Only the new line and the following line required changes.
Public Const VEH_4WD = 1 Four-wheel drive.
Public Const VEH_ABS = VEH_4WD * 2 Anti-lock brakes.
Public Const VEH_Automatic = VEH_ABS * 2 Automatic transmission.
Public Const VEH_Airbag1 = VEH_Automatic * 2 Driver-side airbag.
Public Const VEH_Airbag2 = VEH_Airbag2 * 2 Passenger airbag.
:
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|