Testing a Class

In this lesson, you will learn how to create an instance of a class in order to test the class.

In the past few lessons, you have created a Persons class and given it properties, methods, and events. So far, all you've done is add code—now it is time to use the Persons class and make sure that it works as expected.

Creating an Instance of a Class

Although you may not have realized it, you have been using classes in many of the previous lessons. Forms and controls are actually classes; when you drag a Button control onto a form, you are actually creating an instance of the Button class.

You can also instantiate any class in your code by using a declaration with the New keyword. For example, to create a new instance of the Button class, you would add the following code.

Dim aButton As New Button

To use and test the Persons class, you must first create a test project and add a reference to the class module.

Try It!

To create a test project for your class

  1. Open the Persons project that you created in the previous lesson. If you did not save it, you will first have to go back to the previous lesson, Adding Methods to a Class, and complete the procedures.

  2. On the File menu, point to Add, and then click New Project.

  3. On the Templates pane in the New Project dialog box, click Windows Forms Application.

  4. In the Name box, type PersonsTest and then click OK.

    A new Windows Forms project is added to Solution Explorer, and a new form appears.

  5. In Solution Explorer, select the PersonsTest project, and then on the Project menu, click Set as StartUp Project.

  6. In the Solution Explorer, select the PersonsTest project, and then on the Project menu, click Add Reference.

    The Add Reference dialog box appears.

  7. Click the Projects tab, and then select Persons and click OK.

  8. Double-click the form to open the Code Editor, and then enter the following declaration just under the line Public Class Form1.

    Dim person1 As New Persons.Persons
    

    This declares a new instance of the Persons class. You might wonder why you had to type Persons two times—the first instance is the Persons.vb class module; the second instance is the Persons class within that module.

  9. On the File menu, click Save All.

Testing Your Class

The next step is to add a user interface and code that uses the Persons class. You will add text boxes into which the user will enter values for each of the properties (except the read-only Age property), a check box for the Married field, and buttons to test each of the public methods.

Try It!

To test your class

  1. In Solution Explorer, select Form1, and then on the View menu, click Designer.

  2. From the Toolbox, drag four TextBox controls, a CheckBox control, and two Button controls onto the form.

  3. Select the first Button control, and then in the Properties window set its Text property to Update**.**

  4. Select the second Button control, and then in the Properties window set its Text property to Full Name**.**

  5. Double-click the first button (Update) to open the Code Editor, and then in the Button1_Click event handler, add the following code.

    With person1
        .FirstName = Textbox1.Text
            .MiddleName = Textbox2.Text
            .LastName = Textbox3.Text
            .BirthYear = CInt(Textbox4.Text)
        .Married = CheckBox1.Checked
    End With
    

    Notice that as you type, a list that contains the members of the Persons class is displayed. Since it was added as a reference, IntelliSense displays information about your class just as it would for any other class.

  6. In the Button2_Click event handler, add the following code.

    ' Test the FullName method.
    MsgBox(person1.FullName)
    
    ' test the Age property and CalcAge method.
    MsgBox(CStr(person1.Age) & " years old")
    
    ' Test the Married property.
    If person1.Married = True Then
        MsgBox(person1.FirstName & " is married")
    Else
        MsgBox(person1.FirstName & " is single")
    End If
    
  7. Press F5 to run the project and display the form.

    1. In the first text box, enter your first name.

    2. In the second text box, enter your middle name.

    3. In the third text box, enter your last name.

    4. In the fourth text box, enter the four-digit year of your birth (for example, 1983).

    5. Select the check box if you are married.

  8. Click the Update button to set the properties of the class, and then click the Full Name button.

    Three message boxes are displayed. These message boxes show your full name, your age, and your marital status.

  9. On the File menu, click Save All.

Testing the Overloaded Methods

If you completed the optional lesson Closer Look: Creating Multiple Versions of the Same Method with Overloading, you will also want to test the overloaded methods that you added to the Persons class. If you did not complete the lesson, you can go back and do so now, or you can skip the following procedure.

Try It!

To test the overloaded methods

  1. In Solution Explorer, select Form1, and then, on the View menu, click Designer.

  2. From the Toolbox, drag two more Button controls onto the form.

  3. Select the third Button control, and then in the Properties window set its Text property to With.

  4. Select the fourth Button control, and then in the Properties window set its Text property to Without.

  5. Double-click the first button (With) to open the Code Editor, and then enter the following code in the Button3_Click event handler.

    MsgBox(person1.FirstName & " " & person1.MiddleInitial(True) & 
                               " " & person1.LastName)
    

    Notice that as you type, a list that contains all the members of the Persons class is displayed. Since it was added as a reference, IntelliSense displays information about your class, just as it would for any other class.

  6. In the Button4_Click event handler, add the following code.

    MsgBox(person1.FirstName & " " & person1.MiddleInitial & 
                               " " & person1.LastName)
    
  7. Press F5 to run the project and display the form.

    1. In the first text box, type your first name.

    2. In the second text box, type your middle name.

    3. In the third text box, type your last name.

    4. In the fourth text box, type the four-digit year of your birth (for example, 1983).

    5. Select the check box if you are married.

  8. Click the Update button to set the properties of the class, and then click the With button.

    A message box displays. It shows your name with a period after the middle initial.

  9. Click the Without button.

    A message box displays. It shows your name without a period after the middle initial.

  10. On the File menu, click Save All.

Testing Event Handlers

If you completed the optional lesson Closer Look: Handling Events, you will also want to test the event handlers that you added to the Persons class. If you did not complete that lesson, you can go back and do so now, or you can skip the following procedure.

Try It!

To test the event handler

  1. In Solution Explorer, select Form1, and then, on the View menu, click Code.

  2. Add the statement WithEvents before the person1 declaration, as shown in the following code:

    WithEvents person1 As New Persons.Persons
    
  3. Add the following procedure to Form1. This code checks the calculated age and displays a message based on the result.

    Private Sub person1_AgeCalculated(
        ByVal Age As Integer) Handles person1.AgeCalculated
    
        If Age > 18 Then
            MsgBox("You have been over 18 for " & Age - 18 &
                " years.")
        Else
            MsgBox("You will be 18 in " & 18 - Age & " years")
        End If
    
    End Sub
    
  4. Press F5 to run the project and display the form.

    1. In the first text box, type your first name.

    2. In the second text box, type your middle name.

    3. In the third text box, type your last name.

    4. In the fourth text box, type the four-digit year of your birth (for example, 1983).

    5. Select the check box if you are married.

  5. Click the Update button to set the properties of the class, and then click the Full Name button.

    A message box displays your full name. If you're over 18 years of age, another message box displays information about how long you've been over 18. If you're under 18 years of age, a message box displays information about how long it will be until you turn 18. Then another message box displays your age.

  6. On the File menu, click Save All.

Next Steps

In this lesson, you learned how to create a test project and then use it to test the properties, methods, and events of your class. In the next lesson, you will learn how to use inheritance to create a class based on an existing class.

Next Lesson: Building a Class from an Existing Class: Using Inheritance.

See Also

Tasks

Adding Methods to a Class

Closer Look: Creating Multiple Versions of the Same Method with Overloading