Software Development

  Lecture Notes

Calculator Program Explained - Part 2

These notes examine the calculator program in more detail.  You should have completed the Intro - A  Simple Calculator Program tutorial before working through this.

Introduction

Code Explanation

Extension Work


Introduction

Objectives

This tutorial will enable you to: -  

      become familiar with program code

      become familiar with variables and datatypes

      grow familiar with the Name and Caption property


Code Explanation

The previous tutorial walked you through the creation of a simple calculator program.  You created controls, set some control properties and copied some code into your program to make it react to the user clicking a command button.

Let's examine the code in more detail:-

Starting with the first line, we have:

Private Sub Command1_Click()

Command1 happens to be the name of my command button.   

When a user clicks on this button, VB checks my code to see if I have written code inside a click event sub routine called Command1_Click(). In this case I have, so VB executes all the code inside the sub routine.  

In other words, there is a Click event associated with the command button called Command1, and I have written code for his event.

What do you think would happen if I haven't got a Command1_Click() sub routine and the user clicks the button?

~Now try the activity~

Activity A

  1. Select your command button and change the Name of it to cmdAdd in the property window.  What happens when you run your program and click the Add button.  Nothing right?  Why not?

  1. Now go to the coding window and change your code from:-

Private Sub Command1_Click()

to

Private Sub cmdAdd_Click()

Run your program.  Does it work now? Can you explain why?

Now let's examine the next  three lines:-

Dim firstNumber As Integer
Dim secondNumber As Integer
Dim Result As Integer

When the user enters the two numbers for the program to add together, these numbers need to be stored so they can be added together.  The result of the addition also needs to be stored, ready to display in the answer box.  

Data is stored in memory, and a memory area where a piece of data is stored is called a variable.  We need to be able to refer to memory areas (variables) so we give them a name. In the code above, there are three variables, and I have given them the names firstNumber, secondNumber and  Result.

The word Dim tells VB that you are declaring a variable.

The phrase as tells VB that you expect the variable to be of an integer (whole number) datatypte. You can have other datatypes.  Here are some examples:-

"hello"          this is a string datatype.  You can tell because the word is in between double quote marks ".  In VB, you can declare a string using the phrase As String 

-100             this is an integer datatype because it is a whole number. In VB, you can declare an integer using the phrase As Integer 

10.3             this is a number with a decimal part.  In VB you specify this type of number by using the phrase As Single, or As Double.

When you initially declare a variable, it has no value assigned to it.  You can assign a value to a variable at any time in your code.  Once you have assigned a value to a variable, you are not stuck with that value, you can reassign another value to that variable at any time.

~Now try the activity~

Activity B

1. Examine the variable declaration below;-

Dim num as Integer

Which of the following line assigns an appropriate value to the num variable?

num = 10
num = 10.5
num = "10"
num = "10.5"

Hint: A variable declared as an Integer datatype is given enough space in memory to hold the whole number part of a number.  It does not have enough room to store the decimal part of a number as well?

2. Examine the variable declaration below;-

Dim num as Single

Which of the following line assigns an incorrect value to the num variable?

num = 10
num = 10.5
num = -10
num = "10"

Hint: A variable declared as a Single datatype is given enough space in memory to hold the whole number part of a number and the decimal part of a number.  Does it have enough room to store just the whole number part?

3. Examine the variable declaration below;-

Dim msg as String

Which one of the following lines assigns an appropriate value to the msg variable?

msg = 'error occured'
msg = "error" "occured"
msg = error occured
msg = "-10.5"

Score =
Correct answers:

 

Now let's examine the next  two lines:-

firstNumber = Text1.Text
secondNumber = Text2.Text

Here we are assigning values to the two variables.  The text in the textboxes are placed in the variables firstNumber and secondNumber.

The next line is:-

Result = firstNumber + secondNumber

The values of the two variables firstNumber and secondNumber are added together and the resulting value is placed in the variable called Result.

The next  line:-

Text3.Text = Result

Sets the text in the textbox called Text3 to display the value stored in the variable called Result.

Finally, the  line:-

End Sub

Specifies the end of the click event code for the command button

~Now try the activity~

Activity C

  1. Select your first text box and change the Name of it to txtNum1 in the property window.  

What happens when you run your program. I expect you get an error message!

Then VB will display your code, with the error word Text1 highlighted, like so:-

You don't have have a  textbox with the name Text1 any more.  First stop the program by clicking on the square stop button in VB.  

Then fix the code by typing in the correct name for your textbox and try running your program again.

  1. Now change the name of your second textbox to txtNum2.  Also, change the name of your results textbox to txtResult.  Make sure you also change the code, specifying the new names.


Extension Work

  1. Add another command button to your form and name it cmdSubtract.  Set the Caption property of the command button to Subtract. Double-click the command button, the code window should appear with the following code written for you:-

Private Sub cmdSubtract_Click()

Type in the code so that the numbers in the two textboxes are subtracted, with the result displayed in the third textbox.  It will be very similar to the code for adding the two numbers.  
I'll start you off:-

Private Sub cmdSubtract_Click()
Dim firstNumber As Integer

  1. Add two more command buttons to your form and name them cmdMultiply and cmdDivide.  Set the Caption property of the command buttons to Multiply and Divide.  Double-click each new button in turn and add the correct code for multiplying the two numbers in the textboxes and dividing them.

Your form should look similar to this:-

When you run your program - try dividing the number 5 by 2.  Does your program give the correct answer? How can you correct this.  Examine the datatype for the result variable.  The datatype is an Integer, yes?   Try changing it to a Single.   Does it give you the correct answer now

That's it!!

 

 

 

   

  Unit Information

Assessment

Syllabus

Scheme of Work

Notes &Tutorials

Assignments

Quizzes

Books & Things

Links