Object.Method statement format

Our first program with event handlers has two event handlers:
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.Close()
End Sub

Private Sub btnMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessage.Click
        txtOut.AppendText("Hello, my friend!  ")
End Sub
The instructions in both have the same format:
<object name>.<action>
The name of an object is shown before the dot and the action to be taken is shown after the dot.

Me.Close() tells the computer to close the current form and TxtOut.append("Hello my friend! ") tells the computer to append the text "Hello my friend! " to the Text property of the object called txtOut. When it executes this statement, the computer assigns a new value to the Text property.

These instructions illustrate two new terms, method and argument. We have already seen that an object has a collection of properties like Text and Visible. Objects also have a collection of actions they are capable of executing, and these are called the methods of the object. As we see in this example, a Form is capable of closing itself and a TextBox is capable of appending characters to its Text property. (The term "method" seems like an odd choice to indicate the actions an object can perform, but that is the word programmers use, so we are stuck with it.).

When we use methods in event handlers, the method names are followed by parenthesis containing the values of the method's arguments. The Close method used in this example has no arguments so its parenthesis are empty, but the AppendText method requires an argument specifying the text to be appended. (Programmers borrowed the term "argument" from mathematics).

We can use these terms in rewriting the syntax of our instruction format:

<object name>.<method> ([<argument>])
Note that the square brackets [...] indicate that the argument is optional.

Let's define one related term: member. The members of a class are its methods and properties taken together, so Text and AppendText are both members of the TextBox class.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.