30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

226 Procedures Chapter 6<br />

6.17 Procedure Overloading and Optional Arguments<br />

<strong>Visual</strong> <strong>Basic</strong> provides several ways of allowing procedures <strong>to</strong> have variable sets of parameters.<br />

Overloading allows the programmer <strong>to</strong> create multiple procedures with the same<br />

name, but differing numbers and types of arguments. This allows the programmer <strong>to</strong> reduce<br />

the complexity of the program and create a more flexible application. Procedures also can<br />

receive optional arguments. Defining an argument as optional allows the calling procedure<br />

<strong>to</strong> decide what arguments <strong>to</strong> pass. Optional arguments normally specify a default value that<br />

is assigned <strong>to</strong> the parameter if the optional argument is not passed. Overloaded procedures<br />

are generally more flexible than procedures with optional arguments. For instance, the programmer<br />

can specify varying return types for overloaded procedures. <strong>How</strong>ever, optional<br />

arguments present a simple way of specifying default values.<br />

6.17.1 Procedure Overloading<br />

By overloading, a programmer can define several procedures with the same name, as long<br />

as these procedures have different sets of parameters (number of parameters, types of parameters<br />

or order of the parameters). When an overloaded procedure is called, the compiler<br />

selects the proper procedure by examining the number, types and order of the call’s arguments.<br />

Often, procedure overloading is used <strong>to</strong> create several procedures with the same<br />

name that perform similar tasks on different data types.<br />

Good <strong>Program</strong>ming Practice 6.6<br />

The overloading of procedures that perform closely related tasks can make programs more<br />

readable and understandable. 6.6<br />

The program in Fig. 6.22 uses overloaded method Square <strong>to</strong> calculate the square of<br />

both an Integer and a Double.<br />

1 ' Fig. 6.22: Overload.vb<br />

2 ' Using overloaded methods.<br />

3<br />

4 Public Class FrmOverload<br />

5 Inherits System.Windows.Forms.Form<br />

6<br />

7 Friend WithEvents outputLabel As Label<br />

8<br />

9 ' <strong>Visual</strong> Studio .<strong>NET</strong> generated code<br />

10<br />

11 Private Sub FrmOverload_Load(ByVal sender As System.Object, _<br />

12 ByVal e As System.EventArgs) Handles MyBase.Load<br />

13<br />

14 outputLabel.Text = "The square of Integer 7 is " & _<br />

15 square(7) & vbCrLf & "The square of Double " & _<br />

16 "7.5 is " & square(7.5)<br />

17 End Sub ' FrmOverload_Load<br />

18<br />

19 Function Square(ByVal value As Integer) As Integer<br />

20 Return Convert.ToInt32(value ^ 2)<br />

21 End Function ' Square<br />

Fig. Fig. 6.22 6.22 Overloaded methods (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!