JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Class (Shared) Methods

When you create a class method, you can use it with the class alone, no object needed. For example, in the Shared example on the CD-ROM, I've added a function named Add to the Mathematics class and made it a class method by using the Shared keyword. Now I can use the Add method like this: Mathematics.Add, that is, with the name of the class alone. Here's how I add two integers in text boxes in the Shared example when the user clicks the button with the equals sign (=) caption you see in Figure 11.1:

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = Mathematics.Add(TextBox1.Text, TextBox2.Text)
    End Sub

    Public Class Mathematics
        Shared Function Add(ByVal x As Integer, ByVal y As Integer) _
            As Integer
            Return x + y
        End Function
    End Class

You can see the results in Figure 11.1, where the numbers in the two text boxes are added and the sum displayed when the user clicks the = button. Note that you can only use shared data in a shared method, or the values that are passed to you, as I've done here, unless you provide a specific object to work with.

Related solutions:

Found on page:

Creating Sub Procedures

122

Creating Functions

125

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor