Overloading Methods

Overloading Methods

Another important concept in object-oriented programming is overloading methods. You've been overloading methods all along in your work in classic Visual Basic, but you might not have realized it. Overloading permits a programmer to use the same method name, such as MessageBox.Show, but pass in different parameters at different times. Overloading changes the signature of the method, which consists of the method name and a parameter list.

Let's say you added the following line of code to a button's Click event handler.

MessageBox.Show("To be or not to be...")

The signature here is the method name and a single string parameter. You should see something like what's shown in Figure 2-11. (I know you're cringing and wondering why I'm having you work with something as elementary as messages in a message box. Well, let's just press on and you'll see the method in my madness in a moment.)

Figure 2-11

To be or not to be a form, that is the question.

If you want to display a caption in the message box, you could use the Show method but change the signature to pass two strings, as you can see in the following code and in Figure 2-12.

MessageBox.Show("To be or not to be...", "The Bard")

Figure 2-12

Another method signature; this one includes a caption.

After examining this masterpiece, do you feel there's still room for improvement? Let's add an icon for a visual clue that this text is indeed a question. By modifying the signature of the Show method, you can pass in three parameters: two strings and a constant for the icon. The MessageBox class has several built-in constants that determine the icon the message box displays.

MessageBox.Show("To be or not to be...", "The Bard", _     MessageBoxButtons.OK, MessageBoxIcon.Question)

note

The underscore (_) in the code fragment above is a line continuation character that permits you to continue a code statement on the next line. The Visual Basic .NET compiler treats this as a single line of code.

You can now see the question mark, as shown in Figure 2-13.

Figure 2-13

In the form of a question.

Finally, in case the user isn't in a philosophical mood when the message box is displayed, you decide to give him an option and add the following code:

MessageBox.Show("To be or not to be...", "The Bard", _     MessageBoxButtons.AbortRetryIgnore, _     MessageBoxIcon.Question)

After you make these changes, the message box looks like Figure 2-14.

Figure 2-14

Providing options for the unphilosophical user.

You get the idea. By overloading the Show method of the MessageBox class, the message box takes the appropriate action and invokes the appropriate internal methods to do the right thing. How does it do that?

Seeing the MessageBox Class

Because a message box is really a window, as everything that's visible is to Windows, its class is found in the System.Windows.Forms namespace. Using IntelliSense, you can see that MessageBox is a real class because it has the tri-brick icon. In case you were wondering, the two overlapping rectangles used for some of the other choices is the icon used to designate a constant. Under the hood of a constant is a number. If you've used the Object Browser in classic Visual Basic, you will be no stranger to these icons.

Some of the Overloaded Show Methods

If you were to peek inside the MessageBox class, you would find not one, but several Show methods. Each of them takes a different set of parameters, so each has a different signature. We'll look at the Show method versions illustrated in the preceding section in some more detail here, but the MessageBox class includes additional versions as well. The class contains several function declarations with different parameter lists, but each has the same function name. Each function, however, includes the Overloads keyword.

Overloads Public Shared Function Show(ByVal text As String) _     As DialogResult Overloads Public Shared Function Show(ByVal text As String, _     ByVal caption As String) As DialogResult Overloads Public Shared Function Show(ByVal text As String, _     ByVal caption As String, ByVal style As Integer) _     As DialogResult

The various icons and buttons displayed in a message box are properties of the MessageBox class, as you can see from Figure 2-15. Each of these properties is a constant with an integer value. Notice the IntelliSense display, which provides help on each parameter to the method.

Figure 2-15

A list of the MessageBox class properties and IntelliSense help.

note

You cannot instantiate a new instance of the MessageBox class. To display a message box, you call the class's shared method Show. Because this method is shared, you know that no matter how many message boxes you use, only one Show method is used for each grouping of parameters. The title, message, buttons, and icons displayed in the message box are determined by the parameters that you pass to this method. A very talented lead programmer/architect who works in my department has the first name Joe. About a year ago, we hired another very talented programmer to report to Joe. Unfortunately for us, his name is also Joe. While struggling to differentiate them, I realized that we had overloaded Joes. After several cups of coffee, we resolved the issue by giving the second Joe the nickname Joe++.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net