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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

190 Procedures Chapter 6<br />

In the Function header, the return-type indicates the data type of the result returned from<br />

the Function <strong>to</strong> its caller. The statement<br />

Return expression<br />

can occur anywhere in a Function procedure body and returns the value of expression<br />

<strong>to</strong> the caller. If necessary, <strong>Visual</strong> <strong>Basic</strong> attempts <strong>to</strong> convert the expression <strong>to</strong> the Function<br />

procedure’s return-type. Functions Return exactly one value. When a Return<br />

statement is executed, control returns immediately <strong>to</strong> the point at which that<br />

procedure was invoked.<br />

Common <strong>Program</strong>ming Error 6.4<br />

If the expression in a Return statement cannot be converted <strong>to</strong> the Function procedure’s<br />

return-type, a runtime error is generated. 6.4<br />

Common <strong>Program</strong>ming Error 6.5<br />

Failure <strong>to</strong> return a value from a Function procedure (e.g., by forgetting <strong>to</strong> provide a Return<br />

statement) causes the procedure <strong>to</strong> return the default value for the return-type, often<br />

producing incorrect output. 6.5<br />

6.5 Methods<br />

A method is any procedure that is contained within a class. We have already presented several<br />

FCL methods (i.e., methods contained in classes that are part of the FCL). <strong>Program</strong>mers<br />

also can define cus<strong>to</strong>m methods in programmer-defined classes, such as a class used<br />

<strong>to</strong> define a Windows application. The Windows application in Fig. 6.4 uses two methods<br />

<strong>to</strong> calculate the largest of three Doubles.<br />

1 ' Fig. 6.4: Maximum.vb<br />

2 ' <strong>Program</strong> finds the maximum of three numbers input.<br />

3<br />

4 Public Class FrmMaximum<br />

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

6<br />

7 ' prompts for three inputs<br />

8 Friend WithEvents lblOne As System.Windows.Forms.Label<br />

9 Friend WithEvents lblTwo As System.Windows.Forms.Label<br />

10 Friend WithEvents lblThree As System.Windows.Forms.Label<br />

11<br />

12 ' displays result<br />

13 Friend WithEvents lblMaximum As System.Windows.Forms.Label<br />

14<br />

15 ' read three numbers<br />

16 Friend WithEvents txtFirst As System.Windows.Forms.TextBox<br />

17 Friend WithEvents txtSecond As System.Windows.Forms.TextBox<br />

18 Friend WithEvents txtThird As System.Windows.Forms.TextBox<br />

19<br />

20 ' reads inputs and calculate results<br />

21 Friend WithEvents cmdMaximum As System.Windows.Forms.But<strong>to</strong>n<br />

22<br />

Fig. Fig. 6.4 6.4 Method that determines the largest of three numbers (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!