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.

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

Before discussing classes in detail, we review how <strong>to</strong> add classes <strong>to</strong> a project in <strong>Visual</strong><br />

Studio. By now, you are familiar with adding a module <strong>to</strong> a project. The process of adding<br />

a class <strong>to</strong> a project is almost identical <strong>to</strong> that of adding a module <strong>to</strong> a project. To add a class<br />

<strong>to</strong> a project, select Project > Add Class. Enter the class name in the Name text field and<br />

click the Open but<strong>to</strong>n. Note that the class name (ending with the .vb file extension)<br />

appears in the Solution Explorer below the project name.<br />

The following application consists of class CTime (Fig. 8.1) and module modTime-<br />

Test (Fig. 8.2). Class CTime contains the information needed <strong>to</strong> represent a specific time;<br />

module modTimeTest contains method Main, which uses an instance of class CTime <strong>to</strong><br />

run the application.<br />

In Fig. 8.1, lines 4–5 begin the CTime class definition, indicating that class CTime<br />

inherits from class Object (of namespace System). <strong>Visual</strong> <strong>Basic</strong> programmers use<br />

inheritance <strong>to</strong> create classes from existing classes. The Inherits keyword (line 5) followed<br />

by class name Object indicates that class CTime inherits existing pieces of class<br />

Object. If the programmer does not include line 5, the <strong>Visual</strong> <strong>Basic</strong> compiler includes it<br />

implicitly. Because this is the first chapter that exposes classes, we include these declarations<br />

for the classes in this chapter; however, we remove them in Chapter 9. A complete<br />

understanding of inheritance is not necessary <strong>to</strong> the understanding of the concepts and programs<br />

in this chapter. We explore inheritance in detail in Chapter 9.<br />

1 ' Fig. 8.1: CTime.vb<br />

2 ' Represents time in 24-hour format.<br />

3<br />

4 Class CTime<br />

5 Inherits Object<br />

6<br />

7 ' declare Integer instance values for hour, minute and second<br />

8 Private mHour As Integer ' 0 - 23<br />

9 Private mMinute As Integer ' 0 - 59<br />

10 Private mSecond As Integer ' 0 - 59<br />

11<br />

12 ' Method New is the CTime construc<strong>to</strong>r method, which initializes<br />

13 ' instance variables <strong>to</strong> zero<br />

14 Public Sub New()<br />

15 SetTime(0, 0, 0)<br />

16 End Sub ' New<br />

17<br />

18 ' set new time value using universal time;<br />

19 ' perform validity checks on data;<br />

20 ' set invalid values <strong>to</strong> zero<br />

21 Public Sub SetTime(ByVal hourValue As Integer, _<br />

22 ByVal minuteValue As Integer, ByVal secondValue As Integer)<br />

23<br />

24 ' check if hour is between 0 and 23, then set hour<br />

25 If (hourValue >= 0 AndAlso hourValue < 24) Then<br />

26 mHour = hourValue<br />

27 Else<br />

28 mHour = 0<br />

29 End If<br />

Fig. 8.1 Abstract data type representing time in 24-hour format (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!