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.

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

Performance Tip 8.3<br />

Invocation of the garbage collec<strong>to</strong>r incurs a performance penalty because of such fac<strong>to</strong>rs as<br />

the complex algorithm that determines which objects should be collected. 8.3<br />

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

A call <strong>to</strong> an instance method or an attempt <strong>to</strong> access an instance variable from a Shared<br />

method is a syntax error. 8.8<br />

Normally, the garbage collec<strong>to</strong>r is not invoked directly by the user. Either the garbage<br />

collec<strong>to</strong>r reclaims the memory for objects when it deems garbage collection is appropriate,<br />

or the operating system recovers the unneeded memory when the program terminates. Line<br />

35 uses Public Shared method Collect from class GC of namespace System <strong>to</strong><br />

request that the garbage collec<strong>to</strong>r execute. Before the garbage collec<strong>to</strong>r releases the<br />

memory occupied by the two CEmployee2 objects, it invokes method Finalize for<br />

each CEmployee2 object, which decrements the mCount value by two.<br />

The last two lines of the console output (green window) show that the CEmployee2<br />

object for Bob Jones was finalized before the CEmployee2 object for Susan Baker.<br />

<strong>How</strong>ever, the output of this program on your system could differ. The garbage collec<strong>to</strong>r is<br />

not guaranteed <strong>to</strong> collect objects in a specific order.<br />

1 ' Fig. 8.14: SharedTest.vb<br />

2 ' Demonstrates Shared members.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modSharedTest<br />

7<br />

8 Sub Main()<br />

9 Dim output As String<br />

10<br />

11 Console.WriteLine("Employees before instantiation: " & _<br />

12 CEmployee2.Count)<br />

13<br />

14 Dim employee1 As CEmployee2 = _<br />

15 New CEmployee2("Susan", "Baker")<br />

16<br />

17 Dim employee2 As CEmployee2 = _<br />

18 New CEmployee2("Bob", "Jones")<br />

19<br />

20 ' output of employee2 after instantiation<br />

21 Console.WriteLine(vbCrLf & _<br />

22 "Employees after instantiation: " & vbCrLf & _<br />

23 "via Employee.Count: " & CEmployee2.Count)<br />

24<br />

25 ' display name of first and second employee<br />

26 Console.WriteLine(vbCrLf & "Employees 1: " & _<br />

27 employee1.FirstName & " " & employee1.LastName & _<br />

28 vbCrLf & "Employee 2: " & employee2.FirstName & " " & _<br />

29 employee2.LastName)<br />

30<br />

Fig. 8.14 Shared class member demonstration (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!