8

Visual Basic

Structure and Syntax

Visual Basic structure refers to the way the language is organized and used. The syntax refers to the words used in the language, how they are spelled and how they are used in combination with other words. Like other programming languages, Visual Basic has set ways of doing things. Some are required by the programming environment, and some are used as a convention. A convention is an agreed on method or way of doing things. It is not essential, but programmers have come to adopt them as the standard way of doing it.

The Code Window

Consider the following illustration:

Figure Visual Basic code window

When you open a code window, Visual Basic automatically inserts two lines of code. These lines set the limits of a sub procedure, or subroutine. You cannot insert code outside of these lines. The word Sub indicates the entry point of the procedure and End Sub indicates the exit point. All code placed between these lines is executed whenever the sub procedure is accessed.

Did you notice that the words Private Sub and End Sub are blue (no, they are not links!). As you type code, the Visual Basic text editor checks each and every word and code phrase. Like a smart word processor, the editor operates under specific rules. The text color is a visual clue for special words, comments, and even errors.

Words that appear in blue are called Visual Basic keywords or reserved words. That is,to the Visual Basic editor such words have a very specific meaning and cannot be used in any other way. Therefore when you write code, you cannot use the words private, sub, or end except in the exact way they are intended. So if you write a program to display recipes for sub sandwiches, you couldn't use the word sub as part of your code. Any time one of the words you create turns blue, you know you are using a reserved keyword and your program probably won't work. Visual Basic has many keywords, too many to remember!

While we are on the topic, let's look at two other uses of color in the code window. Consider the following:

Figure Comments in Visual Basic are Green

All programming languages have a way of adding comments or remarks to the code. Comments are not part of the code and have no effect on the program. Comments are added to make the code more understandable both for the programmer and anyone else who reads it.

In traditional BASIC, comments were preceded by the keyword REM which is short for remark. In Visual Basic, Rem or the apostrophe (') can be used as the comment reserved word. When either of ' or Rem is encountered by the Visual Basic editor, all words after it on that line are ignored. The color associated with comments is green.

Finally, if any of your code appears in red it is an indication of an error in the code. In addition, the Visual Basic editor will pop up a message indicating the type of error.

Figure Error Condition Highlighted in Red
Figure Error Message

Statements

Visual Basic statements are simply lines of code that have some specific purpose. Normally statements are written one to a line. It is possible to place more than one statement on a line but they must be separated by a colon (:). Consider the following example

Text1 = "Hello"
Text1.BackColor = VbRed

It can also be written as

Text1 = "Hello" : Text1.BackColor = VbRed

Computers are not intelligent. They can't guess the meaning of a word unless its spelling is perfectly correct and it is used in the exact way intended. The correct spelling and structure of a line of code is referred to as its syntax. For example the code line

VbRed = Text1.BackColor

although quite understandable by us humans this would cause an error in Visual Basic. The error here is actually using the constant name VbRed in the wrong way.

Also it is important to note that many words have different spellings depending on the country, for example colour and color. Visual Basic uses the spellingfound in the American dictionary, not the British or Canadian ones.

Procedures

Event Procedures

All Visual Basic controls have a number of associated or attached procedures. For example, a simple command button has a procedure which is executed anytime there is a mouse click on that button (remember Visual Basic is an event-driven language and clicking a button is an event). There are other procedures linked with the command button which respond to events such as dragging a mouse over the button or pressing a key. Notice that these event procedures start with the word Private (see the illustration above). Private means that the code is only available from that form.

General Procedures

A procedure that is not linked to any control event (eg. a button click) is called a general procedure. Sometimes you may need the same code executed when you cause a number of events to happen. Instead of writing the exact same code a number of times in different control event procedures you can create a general procedure and write the code only once. When this code is needed it can be called from the other procedures. Lets look at a simple example.

Let's say you need a program that can calculate the total cost of a food item complete with sales tax. To make it fast, you assign a food item to each of a group of command buttons. Although each item has a different price, the calculation of the tax is the same.

Figure VB Program to calculate cost
Figure partial code window and form

Explanation of the code

In the example above, one of the event procedures (cmdBigJack) and both general procedures are shown. Note that the event procedure uses the word Private which means that the procedure can only be used on the form that contains it. Also note the event _Click which identifies the type of procedure.

The two general procedures start with the word public which means they can be used from any form in the program. Also note that they do not have an event.

General procedures can be added only if a code window is open. The dialog for adding procedures can be found under the Tools menu.

Figure Add Procedure Option
Figure Add Procedure Window

An executable file for the example shown can be accessed here. Simply click Open when the File Download window opens. Oh, there is no tax on milk :)

Function Procedures

A function procedure is used to do some form of calculation and send back an answer to the original (or calling) procedure. Data are passed to function procedures and they return the result of whatever it is the function does. The actual name of the procedure is used as the variable.

Here is an example. Suppose you need a small program to calculate the hypotenuse of a right triangle. The calculation can be placed as a function and the function given the size of the two sides of the triangle. The function will calculate the hypotenuse and return the answer.

Figure VB Program to Calculate Hypotenuse
Figure Code for the Function Procedure

In this example, the user enters the size of Side a and Side b. When the Hypotenuse button (cmdCalc) is clicked the event code is executed. Consider the line of code indicated by the red dot. This is where the values for Side a and Side b are "sent" to the the public function procedure. The line of code that does the calculation is marked by a green dot. Note that the variable used (hypotenuse) is the same as the name of the function.

An executable form of this example can be accessed here.

Modules

A form and the code associated with the form and any controls on the form are saved in a single file with the .frm extension. It is possible to have a file, that contains only code, as part of a Visual Basic Project. These files are given the name Code Modules and have the file extension .bas. Later you will work with a special code module named inpout32.bas.

Figure Project with extra code module
Naming Conventions

When naming procedures, variables, and constants (more on this later) certain rules (absolute requirements) and conventions (commonly accepted way of doing things) must be observed.

·  Names should start with a letter

·  Names can be 255 characters long

·  Letters, numbers, and the underscore character can be used but not spaces or periods

·  Names cannot be reserved or key words

Recap

Here's a quick review of Visual Basic structure.

·  Visual Basic is an event driven language

·  Visual Basic uses graphical objects called controls

·  Controls have properties that can be changed

·  Controls have associated events (sometimes called methods)

·  When an event occurs, the code in the associated event procedure is run

·  Other procedures can be referenced or called from event procedures

·  General procedures contain code that may be used by a number of controls

·  Function procedures process data and return the results

·  Code modules are files that contain only code and are not associated with forms

Activity

The purpose of the following activity is to investigate the various types of procedures.

Activity 1 VB Editor

Specify the default colors used by the Visual Basic editor for each of the following:

·  Syntax errors

·  Comments

·  Visual Basic Keywords

Activity 2 Code Analysis

The following is the full code listing from the "Cashalator" program. Answer the following questions based on this code:

1.  Give the name of each procedure and identify the type. Example: Command1_Click - Event Procedure

2.  Give the scope of each of the procedures you identified in question 2.

Figure Cashalator Code
Activity 3 Write Cashalator Program

Using the code from Activity 2 and the illustrations from the lesson, create the Cashalator program.