![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Variable names should follow one other guideline. While it may not be an absolute requirement, it is nonetheless important enough to merit the designation of a rule: Use descriptive variable names. This means simply that the name of a variable should describe the data it holds. For example, if your program deals with the population genetics of bats, and you need a variable to hold the number of bats, you should use a name such as Bat_Count or NumberOfBats. You could legally name the variable XY, J89, or BillClinton, and Visual Basic wouldnt care. But when working on the program later, youll find that descriptive variable names make a big difference in the readability of your code. Its a good habit to cultivate right from the get-go. Some programmers believe that descriptive variable naming should be taken a step further so that the name of each variable identifies not only its contents but its type. This is typically done with a type of prefix called Hungarian notation. Using this prefix, the name of an integer variable might be preceded with an i for integer. For your bat count, therefore, you would have a variable named iNumberOfBats. The idea behind Hungarian notation is the ability to discern the variable type immediately whenever you see a variable name in code. In my opinion, there is no need for the inconvenience of Hungarian notation in Visual Basic. The Visual Basic code editor already has a feature that makes it a cinch to determine the type of any variable. Simply right-click on any variable name, then select Quick Info from the pop-up menu. Visual Basic will display a small window with the variable type indicated. You can get the same result by placing the editing cursor on the variable and pressing Ctrl+I. With such easy access to variable types, why bother with the awkward and inconvenient variable names created by Hungarian notation?
How is a variables type assigned? You have two choices. You can explicitly declare the variable in a Dim statement, or you can use a type declaration character at the end of the variable name. An explicit declaration takes the following general form: Dim name As type Heres a specific example: Dim X As Integer, Y As Single, Z As Currency Each variable name is listed, followed by the As keyword and the type name. You can have one or more variables in a single Dim statement, and you can have as many Dim statements as you need, each on its own line. The use of type declaration characters makes Dim statements unnecessary. Ill explain in a minute why I think that the use of type declaration characters is a bad idea, but you do need to know about them. To use the type declaration characters, simply place the character corresponding to the desired variable type at the end of the variable name. The characters are shown in Table 4.2. At this point, because no Dim statement is necessary, you simply use the variable in code as needed. For example, if you need a type Integer variable to hold the value 100, you would write as follows: Count% = 100 In other words, you use the variables only when and where they are neededyou dont have to declare them first. This all sounds quite convenient, but now Ill explain why its a bad idea. To Declare Or Not To Declare The answer to this questionto declare or notis an easy one. Always declare your variables. In fact, you can tell Visual Basic to require variable declaration. Select Options from the Tools menu, click on the Editor tab, and turn on the Require Variable Declaration option. When this option is active, Visual Basic will report a Variable Not Defined error if you try to use a variable that has not been declared in a Dim statement.
Why am I so adamant about always declaring your variables? Why wouldnt the use of type declaration characters be easier? Though it may appear easier at first, it always seems to lead to problems. Heres why. Lets say youve created a variable, but at some point in the code the name is misspelled. Without Option Explicit active, Visual Basic will simply create a new variable using the misspelled name (as far as Visual Basic is concerned, you are creating a new variable). New variables are automatically given the value of zero. Because the program is using this new variable instead of the one it should be using, you are almost sure to get program errors that are extremely hard to track down. If Option Explicit were active, however, Visual Basic would immediately flag the misspelled variable name as an undeclared variable. The problem would never arise. But this isnt the only reason to declare your variables. Some of Visual Basics variable typesByte, Boolean, and Date (Ill get to the last two soon)do not have a type declaration character associated with them. If you want to create a variable with one of those types, you must use a Dim statement. If you are not requiring variable declarations, youll find yourself in a situation where some of your programs variables are declared in Dim statements, while others are not. This is a recipe for confusion, and any seasoned programmer knows potential confusion should be nipped in the bud.
|
![]() |
Products | Contact Us | About Us | |