Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


When you display the contents of a variable that contains Null, it displays the string “Null”.

The third special value that can be stored in the type Variant is Error. As you might expect, this value is used when writing code that deals with errors in the program. I’ll cover it in detail in Chapter 25.


TIP:  Trying Out Code

You can use Visual Basic’s Debug.Print method as a quick way to try out your Basic code. Create a new project, double-click on the form to bring up the Form_Click event procedure, and place the code you want to test in the procedure. Press Ctrl+G to display Visual Basic’s Debug window. (The Debug window is displayed every time you run a program inside the Visual Basic development environment and is hidden when the program ends. To display the Debug window when a program is not running, as instructed here, simply press Ctrl+G.)

To check your code, enter the Debug.Print statement and then enter one or more variable names after the statement, separated by commas. Each Debug.Print statement displays on a new line. For example, the statements

Debug.Print X, Y
Debug.Print Z

will display the values of variables X and Y on one line and the value of variable Z on the next line.


Implementing The Stack

Now let’s get back to implementing the stack for the RPN calculator. Our stack will have two parts: a Variant array where the stack data is kept; and an Integer variable to serve as the stack pointer, indicating the current top of the stack. Let’s see how this works.

Initially, the stack array is empty, and the stack pointer is pointing at element 0. Adding something to the stack requires that we increment the stack pointer so it points at element 1; then we store the data in that element. If we want to store a second item, we again increment the stack pointer by a value of 1, storing the data item in the array element that is now indicated, as shown in Figure 5.1.

Element 0 of the array will never be used in this method, but that’s a small price to pay. If you want to be totally stingy about memory usage, you could use element 0 to store the stack pointer—but we won’t use that approach in the project.

Retrieving data from the stack follows the opposite procedure. To read the item on the top of the stack without removing it, simply retrieve the array element at which the stack pointer points. To remove an item from the stack, you must decrement the stack pointer by 1. Figure 5.2 illustrates the removal of two data items from a stack.

Data items removed from the stack are not actually erased; they remain in the array. They are not part of the stack, however, because the stack pointer is now pointing “below” them. If new items are added to the stack, the old ones will be overwritten.


Figure 5.1  Part A shows an empty stack, Part B shows the stack after adding one item, and Part C shows the stack after adding a second item.


Figure 5.2  Part A shows a stack containing two data items; Parts B and C show the stack after removing one and then both items, respectively.

Creating The Calculator

We’ll begin by creating a basic calculator that can add, subtract, multiply, and divide. Later in the chapter, we’ll add several enhancements, but I think it’s a good idea to start simply. In fact, this is a good approach to use for all of your Visual Basic projects. Make sure the basics of your project are working first, and then add features. By working this way, you can deal with problems one at a time as they arise, rather than face a huge mess of difficulties all at once.

Declaring Public Variables And Constants

Create a new Standard EXE project. Double-click on the blank form to open the Code Editing window. At the top of the editing window, open the Object list (the one on the left) and select (General). If necessary, open the Proc list (the one on the right) and select Declarations. We’ll start by entering the required public variable and constant declarations into the form. The term public means that these data items will be available throughout all parts of the program (the term global is sometimes used in place of public ). I will explain this in detail later in the chapter when I discuss variable scope. For now, enter the code shown in Listing 5.1. Option Explicit should appear by default, but if you have changed any of the default settings and Option Explicit doesn’t appear, make sure to enter this statement at the top of the code window.

Listing 5.1 Variable and constant declarations in the declarations section of the Calculator form.

Option Explicit

‘ The size of the stack.
Const STACKSIZE = 10

‘ The format strings for formatting
‘ numbers in the display.
Const DISPLAY_FORMAT1 = “###,###,###,###.0000000000”
Const DISPLAY_FORMAT2 = “#.######E+000”

‘ The RPN stack; defaults to type Variant.
Dim Stack(STACKSIZE)

‘ Keeps track of current stack location.
Dim StackPointer As Integer

‘ Flag indicating whether we are starting
‘ a new entry.
Dim NewEntry As Boolean

‘ Flag indicating whether the display contains
‘ valid data.
Dim DisplayHasData As Boolean

You may be able to figure out what the various statements do just by reading the comments. But in case it isn’t evident, I will explain them briefly. The code includes three constants: STACKSIZE specifies the number of elements that the stack can hold (I doubt the program will ever need anywhere near 10 elements, but let’s play it safe); the two DISPLAY_FORMAT constants are used in formatting numbers for display by the calculator. I will discuss them in detail later in the chapter.

The first Dim statement declares the array used to hold the stack. Note that the size of the array is specified by the STACKSIZE constant that we declared earlier. The other Dim statements declare a type Integer variable to serve as the stack pointer, and a