Home -> Programming -> Objects -> Using Objects

Using Objects

One of the primary advantages of using objects is that once the blueprint is made, we can reuse our object almost endlessly without writing any more code, but we do have to write the code each time we want to use our object. In this section I will show you what code needs to be written in the main code module to use our previously declared object.

Consider any type of program which keeps track of people. We have written a Person object declaring the variables and the methods we need. Now in our main program we simply have to instantiate (declare) a new object of type Person and all the data and methods are available at our fingertips. Using our event-procedures, selection and loops we can add data to our program limited only by time and disk space. Let's see what these routines would look like for a Contacts program which tracks a person's name, address, phone and email.

Typically we need the following commands, buttons or menu on the main form. For the purposes of this discussion I will assume these are menu commands in a menu called Records. I would build the menu as shown in the following table.

Operation Menu Menu Name
add a new person Contact Menu mnuContactAddNew
edit an existing person mnuContactExisting
remove a person mnuContactRemove
move to the previous person mnuContactFirst
move tot he next person mnuContactLast
move to the first person mnuContactPrevious
move to the last person mnuContactNext

To do this we need to know what kind of an object we have, and what methods are available for us to use. We will assume the object name is Person and that the following methods have been declared and coded:

Now lets see how we would use our Person object to code these menu commands. To make this easier to understand, you can assume we are using this interface.

     Contacts Form

The fields on the form are initially set to be ReadOnly so that the user cannot accidentally modify them. Under these conditions the command mnuRecordAddNew would perform the following tasks:

  1. clear the current display
  2. enable the fields so data can be entered
  3. place the cursor in the first field: FirstName
  4. make sure all menus and buttons are properly enabled or disabled, particularly the Done button which the user clicks when they have finished entering data for the person.

The code for doing these four tasks would be placed in the mnuRecordAddNew.Click event. The code would look like this:

Private Sub mnuContactAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContactAdd.Click
     clear()               'Clears the current display.
     EnableDataEntry()     'A procedure which changes the ReadOnly status of the fields.
     txtFirst.Focus()      'Puts the cursor into the first text field.
     EnableMenus()         'A procedure which updates the enabled property of each menu and button.
End Sub

Now when this menu command is selected the form will change its appearance and look like this:

     Contacts Form

Now we need to code the Done button. This is where we start to use our Person object. First we will make a list of the actions that will be required when the button is clicked. One we have our list of actions we code each action and place them all in the btnDone procedure.

Actions Code
  1. Create an instance of our object.
  2. Assign the data in the text fields into the object.
  3. Get the Age data and display it in the Age field.
  4. Since we will likely enter more than one person, we need a counter to keep track of which person we are presently viewing.
  5. Make the fields Read Only again.
  6. Make sure the menu enabling is current.
Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
    'Add person to collection
    Dim aContact As New Person           'Make instance of Person
    aContact.FirstName = txtFirst.Text   'Assign data to object
    aContact.LastName = txtLast.Text
    aContact.DofB = dtpBirth.Value
    aContact.Phone = txtPhone.Text
    aContact.Cell = txtCell.Text
    aContact.Email = txtEmail.Text
    Contacts.Add(aContact)               'Add contact to list
    lblAge.Text = aContact.Age.ToString  'Get Age and display it
    intCurrent = Contacts.Items.Count - 1'Number of people entered
    DisableDataEntry()                   'Set fields to ReadOnly
    EnableMenus()                        'Set menu enabling
End Sub

Notice the declaration. This is an object definition. It is not quite the same as a variable declaration because it is both a declaration and a line of code. It uses the keyword New which causes space to be allocated for the object. Whereas space allocated for a normal variable remains allocated for the duration of the module in which it is declared, object allocations are only allocated until we tell the program we are finished with the object. Once the object has been declared then we can store data in it. This is done in the 6 assignment statements which follow the declaration. Once the data is stored in the object then we need to put a copy of the object somewhere where we can retrieve it when we need that data back. Contacts would be a module variable declared at the top of the form as a collection. Now we can get the age from the object and assign it to the Age label allowing the user to see the age of our contact. We can now also set the index (intCurrent) which tracks which person we are currently viewing.

Each time we add a new Person we add another object to the program. In this case we add a new variable called aContact. We then store the data entered by the user into this object. Finally we add a copy of the object to a collection (think of it as a list) and we are done. We tidy things up by setting the ReadOnly property of the textboxes to true so that the user cannot accidentally change the data and update the menu and button enabling. At this point the procedure is finished and we exit. The only thing left to understand is what happens to the object named aContact. All local variables are automatically destroyed when the procedure exits but what about objects?