JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Methods

As discussed in the In Depth section of this chapter, methods represent the object's built-in procedures. For example, a class named Animal may have methods named Sleeping and Eating. You define methods by adding procedures, either Sub procedures or functions, to your class; for example, here's how I might implement the Sleeping and Eating methods:

Public Class Animal
    Public Sub Eating()
    End Sub

    Public Sub Sleeping()
    End Sub
End Class

Now I can create a new object of the Animal class and call the Eating method in the familiar way:

Dim pet As New Animal()

In general, you can add whatever functions or Sub procedures you want to your class, including those that accept parameters and those that return values, as we saw earlier in this chapter:

Public Class DataClass
    Private value As Integer

    Public Sub New(ByVal newValue As Integer)
        value = newValue
    End Sub

    Public Function GetData() As Integer
        Return value
    End Function
End Class

Note that these methods are object methods—you use them with an object. However, you also can create class methods. See the next topic for the details.

Related solutions:

Found on page:

Creating Sub Procedures


Creating Functions


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor