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.

Chapter 8 Object-Based <strong>Program</strong>ming 333<br />

11<br />

12 ' construc<strong>to</strong>r of class CCircleConstants<br />

13 Public Sub New(ByVal radiusValue As Integer)<br />

14 RADIUS = radiusValue<br />

15 End Sub ' New<br />

16<br />

17 End Class ' CCircleConstants<br />

Fig. 8.15 Constants used in class CCircleConstants (part 2 of 2).<br />

Module modConstAndReadOnly (Fig. 8.16) illustrates the use of Const and<br />

ReadOnly values. Lines 9–11 use class Random <strong>to</strong> generate a random Integer<br />

between 1–20 that corresponds <strong>to</strong> a circle’s radius. Line 11 passes this value <strong>to</strong> the CCircleConstant<br />

construc<strong>to</strong>r <strong>to</strong> instantiate a CCircleConstant object. Line 13 then<br />

accesses the ReadOnly variable mRadius through a reference <strong>to</strong> its class instance. Lines<br />

15–17 compute the circle’s circumference and assign the value <strong>to</strong> String output. This<br />

calculation employs the Const member PI, which we access in line 17 through its<br />

Shared class reference. Lines 19–20 output the radius and circumference values <strong>to</strong> a<br />

MessageBox.<br />

1 ' Fig. 8.16: ConstAndReadOnly.vb<br />

2 ' Demonstrates Const and ReadOnly members.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modConstAndReadOnly<br />

7<br />

8 Sub Main()<br />

9 Dim random As Random = New Random()<br />

10 Dim circle As CCircleConstants = _<br />

11 New CCircleConstants(random.Next(1, 20))<br />

12<br />

13 Dim radius As String = Convert.ToString(circle.RADIUS)<br />

14<br />

15 Dim output As String = "Radius = " & radius & vbCrLf _<br />

16 & "Circumference = " + String.Format("{0:N3}", _<br />

17 circle.RADIUS * 2 * CCircleConstants.PI)<br />

18<br />

19 MessageBox.Show(output, "Circumference", _<br />

20 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Information)<br />

21 End Sub ' Main<br />

22<br />

23 End Module ' modConstAndReadOnly<br />

Fig. 8.16 Const and ReadOnly class member demonstration.

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

Saved successfully!

Ooh no, something went wrong!