1. Home
  2. Computing & Technology
  3. Visual Basic

Queues and Stacks - Another great tool in VB.NET

The Calculator Program: Improved

By Dan Mabbutt, About.com

The .NET concept of user controls, or controls that you write for your own applications, was featured at About Visual Basic at this link. The idea was to create a single control, four function calculator that can be added to the Visual Basic .NET toolbox. You can pop this control into your own applications and customize it with unique features that you need. For example, you could add a button that plugs company policy amounts into the calculator like allowed daily expense amounts or required internal rates of return.

Great Idea! And the basic concept is as solid as .NET. If you're interested in learning more about custom user controls, I recommend that you read the article.

Unfortunately, the four function calculator itself is, ... ummmm ... how do I put this? Well ... Let's say my mind was focused on user controls when I wrote it.

As it turns out, another new feature of VB.NET can be highlighted by showing you how to improve the basis of the article: the four function calculator. The original was written with a lot of If statements and flags that were constantly being tested to keep track of the "state" of the calculation. So I've revised the calculator program and ...

  • Removed all the problems that used to be there (I think ... let me know if you find one)
  • Reduced the size of the program significantly and also eliminated the flags and conditions

The way I did this was to use one of the two collection classes for maintaining sets of objects to keep track of the numbers and operators that are entered in the calculator keypad. Let's go over that in a little more detail.

When you enter numbers and operands into the keypad of the calculator, somehow, the program has to keep track of whether the first or second number to be added or multiplied has been entered; whether the "Clear" or "Clear Entry" key has been pressed; whether a number is a new calculation or the continuation of one already in progress. This can be done with conditional logic (If-Then-Else), but I think I gave that a pretty good shot in the first article and, trust me, it's hard to get it all exactly right.

A better way is to simply place the numbers and operators (+, -, *, /) that are entered into a FIFO (First In, First Out) Queue and when the = key is pressed, process the queue. This new capability of VB.NET solves a lot of problems.

The VB.NET Stack is a similar class. The main difference is that it's LIFO (Last In, First Out).

Before discussing the program, let's be sure we understand these objects.

Suppose we add "Hitchhiker", 42, and "The Answer" to a VB.NET queue. Conceptually, they will look a lot like an array. But notice that you can add different types to the same queue. Items in a queue or a stack are considered to be objects. The objects are added one at a time with the Enqueue method:

Dim MyQueue as New Queue MyQueue.Enqueue("Hitchhiker")

And they're accessed one at a time with the Dequeue method which removes an element from the queue:

MyString = MyQueue.Dequeue

Or the Peek method which doesn't remove the element:

MyString = MyQueue.Peek

A stack collection has similar methods except that it uses the Push and Pop methods rather than Enqueue and Dequeue. These names are somewhat traditional in other programming languages so VB.NET adopted the same names.

The main difference between a queue and a stack is the order in which things are taken out again. As FIFO and LIFO suggest, a queue is like a line at a store checkout stand. A stack is like one of those columns of plates at a lunch counter. The operating systems of some pretty good computers were based on these simple ideas. (The Digital Equipment DEC PDP line, for example. It was the "PC" of it's day!)

Here's the complete code for the New, Improved Calculator. The form components are identical except that the operator keys are also given Tag properties. Notice that there are now no flags at all. There is no initialization. And there are more common event handlers (such as op_Click which handles all four functions on the calculator).

Neat ... clean ... efficient. Why didn't I think of this the first time?

The original article will be kept on the site (with appropriate comments to let readers know that there is now an improved version available) just to let readers see what a difference the right tool can make.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Queues and Stacks - Another great tool in VB.NET

©2008 About.com, a part of The New York Times Company.

All rights reserved.