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.

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

with class scope, they exist for the duration of program execution, even when no objects of<br />

that class exist. To access a Private Shared class member when no objects of the class<br />

exist, programmers must provide a Public Shared method or property.<br />

A Shared method cannot access non-Shared class members. Unlike non-Shared<br />

methods, a Shared method has no Me reference, because Shared class variables and<br />

Shared class methods exist independently of any class objects and even when there are<br />

no objects of that class.<br />

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

Using the Me reference in a Shared method or Shared property is a syntax error. 8.7<br />

Class CEmployee2 (Fig. 8.13) demonstrates the use of a Private Shared class<br />

variable and a Public Shared Property. The Shared class variable mCount is initialized<br />

<strong>to</strong> zero by default (line 11). Class variable mCount maintains a count of the<br />

number of objects of class CEmployee2 that have been instantiated and currently reside<br />

in memory, including those objects that have already been marked for garbage collection<br />

but have not yet been reclaimed by the garbage collec<strong>to</strong>r.<br />

When objects of class CEmployee2 exist, Shared member mCount can be used in<br />

any method of a CEmployee2 object—in this example, the construc<strong>to</strong>r (lines 14–24) increments<br />

mCount (line 20) and method Finalize (lines 27–32) decrements mCount (line<br />

28). (Note that method Finalize is declared using keywords Protected and Overrides—method<br />

Finalize’s header must contain these keywords, and we will explain<br />

them in detail in Chapter 9.) If no objects of class CEmployee2 exist, member mCount can<br />

be referenced through a call <strong>to</strong> Property Count (lines 53–59). Because this Property<br />

is Shared, we do not have <strong>to</strong> instantiate a CEmployee2 object <strong>to</strong> call the Get method<br />

inside the Property. Also, by declaring property Count as ReadOnly, we prevent clients<br />

from changing mCount’s value directly, thus ensuring that clients can change mCount’s<br />

value only via the class CEmployee2 construc<strong>to</strong>rs and finalizer.<br />

Module modSharedTest (Fig. 8.14) runs the application that demonstrates the use<br />

of Shared members (Fig. 8.13). Lines 11–12 use the ReadOnly Shared Property<br />

Count of class CEmployee2 <strong>to</strong> obtain the current mCount value. Lines 14–18 then<br />

instantiate two CEmployee2 objects, which increment the mCount value by two. Lines<br />

26–29 display the names of the employees. Lines 32–33 set these objects’ references <strong>to</strong><br />

Nothing, so that references employee1 and employee2 no longer refer <strong>to</strong> the<br />

CEmployee2 objects. This “marks” the objects for garbage collection, because there are<br />

no more references <strong>to</strong> these objects in the program.<br />

1 ' Fig. 8.13: CEmployee2.vb<br />

2 ' Class CEmployee2 uses Shared variable.<br />

3<br />

4 Class CEmployee2<br />

5 Inherits Object<br />

6<br />

7 Private mFirstName As String<br />

8 Private mLastName As String<br />

9<br />

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

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

Saved successfully!

Ooh no, something went wrong!