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


  When you have finished this project, you will be able to:
create an Array
add items to an Array
remove items from an Array
use a ComboBox

describe the following terms:

  • array
  • array index
  1. Create a new folder called Array on your personal drive.

  2. The diagram below shows the visual implementation of the Array program.



    The Array Program

    The user enters one number into each of the text boxes and then selects an arithmetic function from the combo box. When the Equals button is pressed, the correct answer is displayed in the txtAnswer TextBox. If the user selects exit, a confirmation dialog box will appear providing a choice of quitting or not.

  3. Start a new Standard.exe project in Visual Basic. Save this project as prjArrayProgram and the form as frmArrayProgram in the Array Program folder.

  4. Use the table below to create the Visual Implementation of the Array Program:

    Objects Table
    Object Property Setting
    Form Name frmArray
      Caption The Array Program
    TextBox Name txtNum1
      Font MS Sans Serif Bold 18
      Alignment Center
      Text Make it blank
      Multiline True
    ComboBox Name cboSign
      Font MS Sans Serif Bold 18
    TextBox Name txtNum2
      Font MS Sans Serif Bold 18
      Alignment Center
      Text Make it blank
      Multiline True
    CommandButton Name btnEquals
      Caption Equals
      Font MS Sans Serif Bold 18
    TextBox Name txtAnswer
      Font MS Sans Serif Bold 18
      Alignment Center
      Text Make it blank
      Multiline True
    CommandButton Name btnExit
      Caption Exit
      Font MS Sans Serif Bold 18

    Note: for the purpose of enhancing the Visual Implementation of the Array Program, you may change other properties such as StartUp Position, background color, etc.

  5. Write the pseudo-code for the Array program in the code view as you create the code.

  6. Now that you have completed the Visual Implementation of the Array Program, you can begin to create the code.

  7. Create the code for the CommandButton btnExit.

  8. An Array is a group of variables all with the same definition. Typically a user selects one of the variables from the list and that selection becomes the variable used in the procedure. For example, the variable might be defined as an arithmetic sign (+,-,X, or ÷). The user would select one of the signs presented and the selected sign would then become the variable that was used. In the Array program, you will use an Array of signs to select the arithmetic function to be performed on two numbers. That is, you will choose to add two numbers, subtract two numbers, multiple two numbers, or divide two numbers.

  9. The actual variable that is selected in the Array is defined by an index of variables that are defined as being part of that array. The indeces for the variables are simple numbers. For example: Note the index starts at 0 rather than 1.

  10. The Array is declared in the Form_Load() procedure. Type the following code to declare the Array:



  11. The first Dim statement defines the number of variables that will be present in the Array - 4 in this case.

  12. The second Dim statement defines the nSignIndex variable - which reads which sign the user selected.

  13. Continue to build the Array code:



  14. The purpose of these four lines of code is to define which sign will be defined by the index numbers of the array.

  15. Continue by adding the next line of code:



  16. The purpose of the cboSign.Clear code is to clear any sign that might be left in the cboSign ComboBox when the program starts.

  17. Finish writing the code for the Array by creating the instructions that will actually display the code when the user clicks the combo box:



  18. Save your project. When you run it, click on the ComboBox, and a selection of arithmetic signs should pop up.

  19. The next step is to write the code of the Equals button. When the Equals button is pressed, the program finds the two numbers the user entered, find the sign the user selected, then performs the appropriate arithmetic function and displays it in the TextBox txtAnswer.

  20. What is the first step involved in creating the code for the Equals button?

  21. Write the code to define the variables for the Equals button. The code goes in the btnEquals_Click() procedure:



  22. As a comment in the sub, write down the purpose of each of the variables.

  23. Next, you need to write the code that does the arithmetic when the Equals button is pressed:



    This code should be typed below the variable declarations for the btnEquals_Click() procedure.

  24. The purpose of the code that you just typed is to find out which sign the user selected by looking at the cboSign index - which you previously defined.

  25. The next code that needs to be written is the code that find out what numbers the user entered into the two TextBoxes, txtNum1 and txtNum2:



  26. When the user entered numbers into the txtNum1 and txtNum2 TextBoxes, Visual Basic read them as a string, not as an integer. Write the following code to convert the string to an integer:



  27. Now, you need to write instructions that will tell Visual Basic what to do with the three variables: Num1, Num2, and nSignIndex.



  28. You just created a series of If . . . Then statements that requires Visual Basic to add, subtract, multiply, or divide the numbers, based on user input. You could, for example, read the first statement as "if the user selected add, then add the first number to the second number." The same is true for all four functions.

  29. The next step is to instruct Visual Basic to place the answer into the txtAnswer TextBox:



  30. Save your project and run it. The program should operate as expected.

  31. You have created an array. An array is simply a variable that contains multiple variables, each defined by an index number. For this first part of this project, you created an array that held variables that you created using code. These variables became part of the array when the program was run.

  32. It is possible to have variables added to, and deleted from, an array while the program is running. In other words, variables could be added to an array by the user, depending upon the user's input.

  33. For the next part of this project, you will create an array of names that can grow and shrink based on the user's input.

  34. Create a new list box on your form. Call the list box lstNames.

  35. Create a new command button called AddItem. Set the appropriate properties for this object; the name, for example should be btnAddItem, and the cation Add Item.

  36. Create a second new command button called DeleteItem. Set the appropriate properties for this object.

  37. Create a text box called Names andset the appropriate properties for it.

  38. Your form should now look similar to the one shown below.



  39. Now that you have the objects created on your form, it is time to create the code for the objects. The first code to write is code that will simply add some names to the list box you just created when the program starts up. Where should this code go?

  40. Add the following code to the Form_Load procedure. This will place five names into the list box when the program starts.



  41. Save your program and run it. The five names should appear in the list box you created.

  42. Next, add code for the Add Item button.



    Your code may be slightly different, depending upon the properties you set for the Add Item button.

  43. Save and run your program. Type a name in the text box, then click the Add Item button. The new name should be added to the list box, and the array index number will be displayed in the text box.



  44. Add five more names to your list box. Notice that once the list extends beyond the boundaries of the list box, that a scroll bar automatically appears.



  45. Exit your program. Do the names that you added appear in the code?

  46. Next, you need to add the code for the Delete Item button.



  47. Save your program and run item. Add some names to the list, then select one of the names and delete it. It should be removed from the list. Notice that you can also remove names from the list that were part of the Form_Load procedure - but these names will re-appear when the program is run again.

  48. You have learned how to create an array, how to add items to an array, and how to remove items from an array. Next you will learn how to extract one of the items from the array and use it in a text box.

  49. Create a new text box called ArrayDisplay, and a command button called DisplayName.

  50. The following code will read the array index, and put the name associated with that index number in the text box.



  51. Save and run your program. Add some names to the list. Select one of the names and click the Display button. The name should appear in the text box.

  52. Be sure to write comments for all of the code you have written for this part of the Array program.

  53. Click here to see the marking guide for this project.

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


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