Ross Sheppard High School
Computer Programming 20
Student Learning Guide
The Variables Program


  When you have finished this project, you will be able to:
declare a variable
describe different types of variable
use a variable

describe the following terms:

  • variables
  • declare
  • integer
  • string
  1. Create a new folder called Variables on your personal drive.

  2. The illustration below shows the visual implementation for the Variables Program.




    The Variables Program


    The user will enter two different numbers; one in each of the two TextBoxes, txtNum1 and txtNum2. When the SumIt! button btnSumIt is pressed the answer will appear in the TextBox txtAnswer. When the CommandButton btnExit is pressed the program will end.


  3. Start a new Standard.exe project in Visual Basic.

  4. Use the table below for the visual implementation of the Variables program:

    Object

    Property

    Setting

    Form

    Name

    frmVariables

     

    Caption

    The Variables Program

    TextBox

    Name

    txtAnswer

     

    Text

    Make it blank

     

    Font

    Ms Sans Serif Bold 18pt

    TextBox

    Name

    txtNum1

     

    Text

    Make it blank

     

    Font

    Ms Sans Serif Bold 18pt

    TextBox

    Name

    txtNum2

     

    Text

    Make it blank

     

    Font

    Ms Sans Serif Bold 18pt

    Label

    Name

    lblSign

     

    Caption

    +

     

    Font

    Ms Sans Serif Bold 18pt

     

    Alignment

    2- Center

    CommandButton

    Name

    btnSumIt

     

    Caption

    Sum It!

     

    Font

    Your choice

    CommandButton

    Name

    btnExit

     

    Caption

    Exit

     

    Font

    Your choice

  5. Save this project in the Variables Program folder as Variables Program. It should look similar to the illustration below:



  6. Add the following comment to the General_Delcarations() area of your form:



  7. Now it is time to enter the code for the Variables Program. Start by entering the code for the Exit Button:



  8. When you have input from the user in a program, you require a variable. You also require a variable anytime anything in the program changes. In the Hello World project there were no variables, so you didn't need to learn to work with them. However, this program does have variables. Do you know what the variables are?

  9. Go to the Code View window of your Variables Program project.

  10. In the left hand box of the Code View, select General and in the right hand box select Declarations. The General Declarations is where code that applies to the entire program is placed. In this case you will write code that requires all variables to be declared, thus preventing mistakes when you run the program.

  11. Enter the following code:



  12. The Option Explicit statement means that Visual Basic will not accept any varibles that have not been declared. Using this statement is good programming convention and will save you many hours of trouble shooting later on.

  13. To ensure this statement is added to every new project, click on Tools in the Menu Bar then Options . . .

  14. The window shown below will appear; check the Require Variable Declaration as shown in the illustration below:



  15. Next, declare the variables that the user will enter in the TextBoxes txtNum1 and txtNum2.
    The variables will be used when the CommandButton btnSumIt is pressed. Therefore, the variables should be declared during that event.


  16. In the View Code window, select the btnSumIt_(Click) procedure and enter the following code:



  17. Save your Variable Program and run it. Does it work?

    A note about variables:

    Syntax for declaring a variable:

    Dim varName As Datatype

    Where:
  18. In addition to the Integer variable there are a number of other types as shown in the table below:

    Variable Type

    Code

    Example

    Integer

    Whole numbers
    2 bytes allocated

    Integer to hold a person's age
    Dim nAge As Integer

    Long

    Whole numbers
    4 bytes allocated

    Long number to count the population of Canada
    Dim nCanadianPopulation As Long

    Single

    Decimal

    Decimal point number to answer a math problem
    Dim sAnswer As Float

    Double

    Decima to 12 digits of accuracy

    Double to record salaries
    Dim fSalary As Double

    Boolean

    True/False statements

    Boolean as in if you pass this course or not
    Dim bPassed As Boolean

    String

    Text Data

    String to hold your name
    Dim strFirstName As String

  19. When a user first starts a program it is expected that the cursor will be in the first place that data will need to be entered. In the case of your Variables Program, the user is expected to enter a number into the TextBox txtNum1.

  20. When the Variables Program is first started, it may not default to the txtNum1 TextBox, so as a programmer, you must set the place where the cursor starts. This is called setting the Focus.

  21. For this project, you will set the focus during the loading of the form using the TabIndex procedure. Go to the Code View window. Create a new sub by selecting Form in the object box and Load in the Procedure box.



  22. This will add a new sub to the code window:



  23. Add the following code to make the txtNum1 TextBox as the place where the cursor will appear when the form loads:



  24. While you are at it, add the code so that when the user presses the Tab key, the focus will change to the txtNum2 TextBox.

  25. Note that you could also set the tab order when you create the object by changing it in the properties box:



  26. For all projects that you create in Visual Basic, you will be expected to set the focus in the appropriate place.

  27. Write the pseudo-code for the Variables Program directly in your program as comments.

  28. Be sure to add the tool tips to this and all subsequent programs.

  29. Click here to see the marking guide for the project.

  30. When you have finished, send an email to your teacher asking that this project be marked.


© 1998-2000 N.F. Mathew, EdD
File name: Variables.htm
Last updated on October 18, 2000