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.

230 Procedures Chapter 6<br />

7 Friend WithEvents txtBase As TextBox ' reads base<br />

8 Friend WithEvents txtPower As TextBox ' reads power<br />

9<br />

10 Friend WithEvents inputGroup As GroupBox<br />

11<br />

12 Friend WithEvents lblBase As Label ' prompts for base<br />

13 Friend WithEvents lblPower As Label ' prompts for power<br />

14 Friend WithEvents lblOutput As Label ' displays output<br />

15<br />

16 Friend WithEvents cmdCalculate As But<strong>to</strong>n ' generates output<br />

17<br />

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

19<br />

20 ' reads input and displays result<br />

21 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _<br />

22 ByVal e As System.EventArgs) Handles cmdCalculate.Click<br />

23<br />

24 Dim value As Integer<br />

25<br />

26 ' call version of Power depending on power input<br />

27 If Not txtPower.Text = "" Then<br />

28 value = Power(Convert.ToInt32(txtBase.Text), _<br />

29 Convert.ToInt32(txtPower.Text))<br />

30 Else<br />

31 value = Power(Convert.ToInt32(txtBase.Text))<br />

32 End If<br />

33<br />

34 lblOutput.Text = Convert.ToString(value)<br />

35 End Sub ' cmdCalculate_Click<br />

36<br />

37 ' use iteration <strong>to</strong> calculate power<br />

38 Function Power(ByVal base As Integer, _<br />

39 Optional ByVal exponent As Integer = 2) As Integer<br />

40<br />

41 Dim <strong>to</strong>tal As Integer = 1<br />

42 Dim i As Integer<br />

43<br />

44 For i = 1 To exponent<br />

45 <strong>to</strong>tal *= base<br />

46 Next<br />

47<br />

48 Return <strong>to</strong>tal<br />

49 End Function ' Power<br />

50<br />

51 End Class ' FrmPower<br />

Fig. Fig. 6.24 6.24 Optional argument demonstration with method Power (part 2 of 2).

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

Saved successfully!

Ooh no, something went wrong!