Adding Methods to a Class

In this lesson, you will learn how to add methods to a class so that it can perform actions.

In an earlier lesson, Closer Look: Understanding Properties, Methods, and Events, you learned that most objects have actions that they can perform; these actions are known as methods. The Persons class that you created in the Modeling a Real-World Object: Creating Your First Class lesson represents a person. There are many actions that people can perform, and for the Persons class, those actions can be expressed as class methods.

Class Methods

The methods of a class are just Sub or Function procedures declared in the class. For example, an Account class might have a Sub procedure named Recalculate that would update the balance, or a CurrentBalance Function procedure to return the latest balance. The code to declare those methods might resemble the following:

Public Sub Recalculate()
    ' add code to recalculate the account.
End Sub
Public Function CurrentBalance(ByVal AccountNumber As Integer) As Double
    ' add code to return a balance.
End Function

While most class methods are public, you might also want to add methods that can only be used by the class itself. For example, the Persons class might have its own function for calculating a person's age. If you declare the function as Private, it cannot be seen or called from outside the class.

The code for a private function might resemble the following:

Private Function CalcAge(ByVal year As Integer) As Integer
    CalcAge = My.Computer.Clock.LocalTime.Year - year
End Function

You could later change the code that calculates the value of CalcAge, and the method would still work fine without changing any code that uses the method. Hiding the code that performs the method is known as encapsulation.

In the Persons class, you will create a public method that returns a full name, and a private function to calculate the age.

Try It!

To add a method to 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 Properties to a Class, and complete the procedures.

  2. In Solution Explorer, select Persons.vb, and then on the View menu click Code.

  3. Add the following code underneath the property procedures.

    Public Function FullName() As String
        If middleNameValue <> "" Then
            FullName = firstNameValue & " " & middleNameValue & " " &
                                      lastNameValue
        Else
            FullName = firstNameValue & " " & lastNameValue
        End If
    End Function
    
    Private Function CalcAge(ByVal year As Integer) As Integer
        CalcAge = My.Computer.Clock.LocalTime.Year - year
    End Function
    
  4. Modify the code in the Age property procedure to use the private function.

    ReadOnly Property Age() As String
        Get
                  ' Return My.Computer.Clock.LocalTime.Year - birthDateValue
                  Return CStr(CalcAge(birthYearValue))
        End Get
    End Property
    
  5. On the File menu, click Save All to save your work.

Next Steps

In this lesson, you learned how to add both public and private methods to a class. You can learn more about methods in Closer Look: Creating Multiple Versions of the Same Method with Overloading, or you can go on to the next lesson and learn how to add events to your class.

Next Lesson: Adding Events to a Class.

See Also

Tasks

Adding Properties to a Class

Other Resources

Programming with Objects: Using Classes