|
 |
 |
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 5 Visual Design + Basic Code = Visual Basic
By adding a few more fundamentals to what you have learned, youll be well on your way to becoming captain of the good ship Visual Basic.
Weve covered a lot of ground in the first four chapters, and I hope youre beginning to get your Visual Basic sea legs. If Ive been doing my job well, youre starting to develop at least a hazy picture of how the various parts of Visual Basicobjects, properties, and codework together to form a fully functioning application program. To bring this picture into sharper focus, were going to tackle a somewhat more ambitious project in this chapter: an on-screen calculator. As you work through this project, you will see how everything Ive crammed into your head in the past four chapters comes together. Along the way, Ill present a few more useful Visual Basic tools and techniques.
Planning The Calculator
The first step in planning a calculator is deciding what type of logic it will use. Calculator logic comes in two types: algebraic (the more common) and reverse Polish notation (RPN). With an algebraic calculator, you perform calculations electronically in the same way you would on paper. For example, to add 4 to 7 on paper, you would write:
4 + 7 = 11
Using an algebraic calculator for this task, you would press 4, press +, press 7, and press =, and the answer would be displayed. In contrast, to perform this calculation using an RPN calculator, you would press 4, press Enter, press 7, then press +. If you are not accustomed to RPN calculators, this probably seems weird to you. It makes a lot of sense, however, and is actually quite similar to internal computer logic. Lets take a look at how it works.
RPN And Stacks
RPN is based on the concept of a stack, which is a form of data storage using the last in, first out method. In other words, when you retrieve a data item from the stack, youll get the item that was most recently placed on the stack. If you retrieve another item, youll get the one before that, and so on. The most common analogy to a data stack is the way cafeteria plates are often stacked in a spring-loaded tube. As you retrieve plates, they come out in the reverse order in which they were inserted. Heres what happens, then, when you add 4 and 7 on an RPN calculator:
- Press 4 Puts 4 in the display.
- Press Enter Completes entry of the first number.
- Press 7 Moves the first entry (4) to the top of the stack and puts 7 in the display.
- Press + Adds the number in the display (7) to the number at the top of the stack (4), displays the result, and removes the top number (4) from the stack.
For short calculations, the RPN method really has no advantage over the algebraic method. Its the longer calculations where it comes into its own. Lets say you want to add 4 to 7, multiply the result by 2.5, then square that result. Table 5.1 shows how you would enter this calculation, and how it would show in the display.
Table 5.1 The steps in an RPN calculation.
|
|
Press
| Display Reads
|
4
| 4
|
Enter
| 4
|
7
| 7
|
+
| 11
|
2.5
| 2.5
|
x
| 27.5
|
2
| 2
|
Yx
| 756.25
|
|
On an algebraic calculator, not only would this equation require a lot more keystrokes, but the order of execution would be more difficult to follow. In any case, Im not here to sell you on the advantages of an RPN calculator, but we will be using RPN logic for our project. If you want a project to try on your own, work on creating an algebraic calculator. Youll quickly see which type of calculator is easier to program.
Before we get started on the calculator, lets take a closer look at Visual Basics Variant data type, as we will be using it in the project.
The Variant Data Type
Most of the data types we examined in the previous chapters have been rather specificthat is, they could hold either string or numeric data, and the numeric types could hold either integer or floating point values. The Variant type is a different animal altogether. A type Variant variable can hold just about any kind of datastring, integer, or floating point. Even more amazing (at least to experienced programmers who are used to the standard fixed data types), the data in a type Variant is automatically treated in the appropriate way.
What does this mean? For the most part, it means that the data is treated as either a string or a number, depending on how you are using the variable. Heres an example. Suppose that MyVariant is a type Variant, and you assign data to it as follows:
MyVariant = 1234
If you add this variable to a numeric variable, the data will then be treated as a number. Thus, after the following statements execute (assuming X and Y are type Integer )
X = 111
Y = X + MyVariant
the variable Y will contain the numeric value 1345. Now, if S is a type String variable, then after executing the statement
S = abc & X & def
the variable S will contain the string abc1234def. Throughout the book, you will see that the Variant type can be very useful when your program is working with numeric data that must also be treated as a stringformatting data for display in a calculator, for example. A type Variant can also be used to hold an object reference, as we will see in Chapter 7, but this is not relevant to the current project.
|