Chapter 2 Adding comments to your code

As a computer programmer, it is very important to keep a record of what you have done, and other information that relates tou your program. When this information is keept with the code, it is called internal documentation.

There major variations in the Visual Basic code to the example in Chapter 1 is the internal documentation or comments. A single quote (') is used to make a comment about a part of the program. These are used to explain parts of the program coding or to put information into the code for yourself or others to read. Comments are ignored by the computer during processing and are used to help to make the code more readable.

Tips for writing good code for programs

Two programs may appear to do the same thing but one might be much better written than the other. The following is a guidelines to creating the code for programs.

Planning

Using a simple sequence of steps, it is possible to construct a range of very useful programs. The planning stage is considered to be very important when creating a computer program because it is here that the programmer decides the steps that the computer must follow to solve the problem.

Problem 2

Ace Auto's pays its sales team a weekly retainer of $600 plus 2% commission on all sales made. You have been commisiioned to write a Visual Basic program that will allow the finance department to calculate the weekly salary for each salesperson.

A Visual Basic Solution is shown below.

Planning

Output

The computer must show the salary of the salesman

Input

The computer must have the value of the sales to do the calculations. The name of the salesperson must also be given although there is no calculations on this..

Processing

The calculation of the salary involves finding 2% of the sales and adding $600.

Forms and Controls

This application only needs one form. By setting the properties of the form and each control, the screen above can be duplicated.

Forms

 

Property

Setting

Form BackColor (Blue)
  Caption Ace Autos
  Name Form1

Controls

This applications needs six Labels, one Image, two TextBoxes and two CommandButtons. The properties and settings for one of each control is shown below.

Object

Property

Setting

Image Name ImgLogo
  Picture Bitmap
Label Name LblHeading
  BackStyle 0-Transparent
  Caption Ace Autos
  Font Ariel Black
  ForeColor (Red)
TextBox Name TxtSales
  Text (empty)
  Font Ariel Black
CommandButton Name CmdCalculate
  Caption &Calculate
Label Name LblOut
  Caption (empty)
  Font Ariel Black

Coding

Pseudocode .

Get employees name
Get value of the sales
Set Salary To Sales x Commisssion/100 + Retainer
Write Employee name and Salary

Code

A Visual Basic program which could be created using this plan is as follows:

Private Sub CmdCalculate_Click()
  'Author Mike Leishman
  'Date 03/12/97
  'Purpose Calculate the Salary of an employee working for commission

  Const Commission = 2
  Const Retainer = 600

  Dim Employee As String
  Dim Sales As Long
  Dim Salary As Long

  'Data Input
  Employee = TxtName.Text
  Sales = TxtSales.Text

  'Salary Calculation
  Salary = Retainer + Sales * Commission

  'Information Output
  LblOut.Text = "$" & Salary

End Sub

Problems

  1. Padbury Milk delivers milk, cream and juice everyday to the people in Padbury. A weekly account is made and delivered on each Friday. The prices are per carton: milk $2, cream $4 and Juice $3. Write a Visual Basic program to accept the name of the customer and the number of cartons of each product per week. It must then calculate the total amount owing.
  2. Paint'a'Drive is a paint that can be applied to driveways to renew the surface. Jone's Painting Services will paint a driveway for $15 a square meter and a set fee of $75. Mr Jones wants to have a computer program to calculate of the amount of Paint'a'Drive needed. Make a program that will accept the length and width of a driveway. It must then calculate the area of the drive and determine the cost of painting the surface.
  3. Fred's Fencing erects multi-strand wire fences around small fields. Fred knows the lenght and width of the field (in meters) and the number of strands needed. Write a program for Fred so as he will know how much wire he needs to buy to put up a fence.

Author: Mike Leishman

Last Modified June 8 1998