MessageBox, a class with objects created at run time.

Not only are MessageBoxes handy and used in many programs, they will allow us to illustrate several new ideas.

One of these is that MessageBoxes are created during execution, at run time, not by adding objects to the user interface at design time. There is no MessageBox icon in the development system Toolbox.

Consider this one-line greeting program:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   MessageBox.Show("Hiya kids!  Hiya! Hiya!")
End Sub
When the Show method is executed, a small window opens and displays the message. In this example, Show has one argument, a string expression which is displayed in the MessageBox. The syntax is:
MessageBox.Show (<string expression>)

The MessageBox class differs from Button, Label and others we have used in another way. The other methods were called with the syntax:

<object>.<method>
but MessageBox uses the syntax:
<class>.<method>
The Show method is associated with the class itself rather than an instance of the class. This is an example of a shared method, and many other classes have them. (See, for example, our note on methods of the Math class).

Note that there are several variations on the Show method. For example, it can be called with two arguments instead of one. Changing the event handler to:

MessageBox.Show("Hiya kids!  Hiya! Hiya!", "Goofy greeting")
adds a title to our greeting.

The two arguments are separated by a comma, and commas are used to separate multiple arguments regardless of how many there are.

When a method can accept a variable number of arguments or the data types of the arguments can vary, it is said to be overloaded.


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.