|
 |
 |
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!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
Chapter 4 The Basics Of Basic
Underneath Visual Basics fancy objects and events lies a foundation of Basic code doing the grunt work. Lets take a comprehensive look at just what Basic is and what you can do with it.
Someday, you might be able to create a complete applications program by doing nothing more than combining a few software components, but that day has yet to arrive. By now, I hope you are convinced of the power and flexibility of Visual Basics objects. Despite all this power, Visual Basic still requires some glue to stick the objects together into a functioning program. As you saw in the example in the previous chapter, that glue is Basic code.
What exactly is Basic? The name is an acronym for Beginners All-Purpose Symbolic Instruction Code, a language that was originally developed for instructional purposes many moons ago at Dartmouth College. While the original Basic may have been ideal as a teaching aid, for many years it had a reputation among programmers for being slow and inflexible. The reason for this negative connotation was that Basic was slow and inflexible. Greasy kid stuff, snorted the serious programmers as they went off to struggle with C or FORTRAN.
Over the years, however, Basic gradually improved. With Microsoft QuickBasic leading the way, Basic evolved from a language suited only for students and hobbyists to a powerful tool that was up to the task of creating full-fledged business and scientific applications. And this, my fellow programmers, is the Basic that youll discover inside Visual Basic. It is a powerful, flexible, and efficient language capable of handling just about any programming task you ask of it. The fundamentals of the language are fairly easy to learn; you will do so in this chapter.
We wont be covering the complete Basic language here. That could beand, in fact, has beenthe subject of complete books. If I tried to squeeze any sort of complete coverage of Basic into one chapter, I would end up with a very long and boring chapter. Instead, Ill cover what I feel are the real basics (again, no pun intended) of the language: the tools you will need to get started writing Basic code. Then, as more advanced language topics arise during the remainder of this book, Ill provide you with more detailed information.
Where Did I Put That Data?
Almost every program works with data, or information, of one kind or another. Whether the data is a chunk of text to be edited, a graphic image, or a table of numbers, the program needs a place to keep the data while the program is executing. Im not talking about disk files here, although thats an important topic in its own right. Im referring to something else. Permit me to explain.
During execution, a program stores its data in variables and, to a lesser degree, constants. A variable derives its name from the fact that its stored information can change, or vary, during program execution. In contrast, the information stored in a constant does not change. Visual Basic offers a rich variety of variables and constants to suit all your programming needs.
Visual Basics variables can be divided into three general categories: Numeric variables store numbers; string variables store text; and the third category consists of data types that cannot be clearly categorized as either string or numeric. Each of these basic categories is further subdivided. Lets start with the numbers.
Numeric Variables
Working with numbers is something that computers do frequently, so we certainly need a type of variable to store them. Visual Basic actually has several types of numeric variables: Byte, Integer, Long, Single, Double, and Currency (more on these variable types in a moment). Why not just have a single numeric variable that can hold any number? Efficiency. The number 5 can be stored in less memory space and manipulated more quickly than 123,513,465,760.74666. By providing several subtypes of numeric var-iables, each suited for a certain numerical range and degree of accuracy, program efficiency can be improved.
Numbers can be divided into two types: integers, which have no fractional part (digits to the right of the decimal point); and floating point numbers, which can, but do not have to, contain a fractional part. The three integer variable types are called Byte, Integer, and Long; the three floating point types are referred to as Single, Double, and Currency. These types all differ in the ranges of valuesthe largest and the smallestthey can hold. The floating point types also differ in their precision, or accuracy, which refers to the number of digits to the right of the decimal point. The sixth type of numeric variable, Currency, is specifically designed to work with financial figures. As Table 4.1 shows, Visual Basic numeric variable types provide for just about any imaginable situation.
How do you decide which type of numeric variable to use in a particular situation? The general rule is to use the smallest type that will suffice. For an integer variable, use a type Integer in preference to a Long, but only if youre sure that the data stored there will never exceed the limits of a type Integer. Because of its extremely limited range, type Byte can be used only in special situations (but can be very useful indeed). Similarly, use a Single in preference to a Double whenever possible. Currency is usually the best choice when youre working with money amounts.
The next logical questions are: How do you create variable names? and How do you inform Visual Basic of the type of variable you want? Well address these questions in our next topic.
Naming And Declaring Variables
Each variable that you use in your program must have a unique name. Several rules apply to creating variable names:
- The maximum length is 255 characters.
- The first character must be a letter.
- The name cannot contain a period, space, or any of the following characters: ! @ # & % $.
Table 4.1 Numeric variables.
|
|
Variable Type
| Minimum Value
| Maximum Value
| Precision
| Storage Space
|
Byte
| 0
| 255
| N/A
| 1 byte
|
Integer
| -32,768
| 32,767
| N/A
| 2 bytes
|
Long
| -2,147,483,648
| 2,147,483,647
| N/A
| 4 bytes
|
Single
| -3.4 x 1038 *
| 3.4 x 1038*
| 6 digits
| 4 bytes
|
Double
| -1.79 x 10308 *
| 1.79 x 10308 *
| 14 digits
| 8 bytes
|
Currency
| -9.22 x 1011 *
| 9.22 x 1011 *
| 4 digits
| 8 bytes
|
*An approximate value. See Visual Basic Help for exact figures.
|
|
|