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 329<br />

10 ' number of objects in memory<br />

11 Private Shared mCount As Integer<br />

12<br />

13 ' CEmployee2 construc<strong>to</strong>r<br />

14 Public Sub New(ByVal firstNameValue As String, _<br />

15 ByVal lastNameValue As String)<br />

16<br />

17 mFirstName = firstNameValue<br />

18 mLastName = lastNameValue<br />

19<br />

20 mCount += 1 ' increment shared count of employees<br />

21 Console.WriteLine _<br />

22 ("Employee object construc<strong>to</strong>r: " & mFirstName & _<br />

23 " " & mLastName)<br />

24 End Sub ' New<br />

25<br />

26 ' finalizer method decrements Shared count of employees<br />

27 Protected Overrides Sub Finalize()<br />

28 mCount -= 1 ' decrement mCount, resulting in one fewer object<br />

29 Console.WriteLine _<br />

30 ("Employee object finalizer: " & mFirstName & _<br />

31 " " & mLastName & "; count = " & mCount)<br />

32 End Sub ' Finalize<br />

33<br />

34 ' return first name<br />

35 Public ReadOnly Property FirstName() As String<br />

36<br />

37 Get<br />

38 Return mFirstName<br />

39 End Get<br />

40<br />

41 End Property ' FirstName<br />

42<br />

43 ' return last name<br />

44 Public ReadOnly Property LastName() As String<br />

45<br />

46 Get<br />

47 Return mLastName<br />

48 End Get<br />

49<br />

50 End Property ' LastName<br />

51<br />

52 ' property Count<br />

53 Public ReadOnly Shared Property Count() As Integer<br />

54<br />

55 Get<br />

56 Return mCount<br />

57 End Get<br />

58<br />

59 End Property ' Count<br />

60<br />

61 End Class ' CEmployee2<br />

Fig. 8.13 CEmployee2 class objects share Shared variable (part 2 of 2).

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

Saved successfully!

Ooh no, something went wrong!