These
notes examine the calculator program in more detail. You should have
completed the Intro
- A Simple Calculator Program tutorial before working through
this.
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 |
-
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?
-
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~
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~