Program 1 - simple maths
Back to Main Page
In this first program we are going to write code to add 2 numbers together

After doing this you will be able to:
- open VB
- enter code into the code window
- modify the code to change what it does
- save your program to floppy disk
- print out a copy of your work for reference
- re-open a saved file

Task 1
- Look at the VB code below
- Read the 'what the code does' section underneath the code carefully
What the code does:

Const Firstnumber = 5 - creates a quantity called Firstnumber and sets it to 5

Const Secondnumber = 2 - creates a quantity called Secondnumber and sets it to 2

Dim Total as Integer - creates a space in memory for a whole number (or integer) to be stored. Total will store our answer to the sum  - we need to have a space in memory reseved and give it a name before we do anything else.
-this will be explained fully later, so don't worry!!

Total = First number + Secondnumber
- adds the FirstNumber and SecondNumber together (5 + 2)
- stores the result in the memory location called 'Total'

Form1.Show
- opens up a form to display the results - in this unit, we will use forms as a way of giving our user instructions and displaying the results of the program to them.

Form1.print "The total is " ; Total

- prints out onto the form the words "The total is" THEN puts the value of Total (i.e. 2+5=7)

Task 2
- Open VB - follow the instructions on the page
"Setting up VB"
- Enter the code above between the lines Form Load() and End Sub

Task 3
- Run your program by clicking the run button in VB's top bar
- VB will highlight any errors in
yellow - check for typing mistakes!
- You should see a grey form with the words "The total is 7"
- Click Close (X) to return to the code window
Enter this code for Task 2
Task 4 - Changing the code

Make the following alterations to your code:

1. Change the first number to 10
2. Change the second number to 5
3. Change the code so that it SUBTRACTS the 1st number form the 2nd
4. Change the message on the form to make the message to the user:
                        "The answer to the subtraction is"


Task 5
- Save you work to floppy disk
This is a 2 stage process:
1. Save the FORM you have created
2. Save the VB project - this saves your code

- Click on
File menu -> Save project as
  
a window will open with the caption 'Save form as'
- Save the form (.frm file) to the A drive, calling it
maths1
   a new window will now open captioned '
Save project as'
- Save the project (.vbp file) to the A drive, calling it
maths1


Task 6 - printing your work

- Click on File Menu -> Print
- Choose current module and Code in the tick boxes
- Click OK


Task 7 - re-opening a saved file

- Close VB
- Re-open VB - click cancel if offered the 'open standard exe' window
- Click on
File menu - Open Project (or the folder icon in the top bar)
- Select the
A drive in the 'Look in' window
- Click on the
maths1.vbp file
- Run you program to check that it works correctly


End of tasks
Hosted by www.Geocities.ws

1