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

inherit method Finalize and can override it <strong>to</strong> free resources specific <strong>to</strong> those classes.<br />

The overridden method is called before garbage collection occurs—however, we cannot<br />

determine exactly when this method is called, because we cannot determine exactly when<br />

garbage collection occurs. We discuss method Finalize in greater detail in Chapter 9,<br />

when we discuss inheritance.<br />

8.11 Shared Class Members<br />

Each object of a class has its own copy of all the instance variables of the class. <strong>How</strong>ever,<br />

in certain cases, all class objects should share only one copy of a particular variable. A<br />

Shared class variable is such a variable; a program contains only one copy of this variable<br />

in memory, no matter how many objects of the variable’s class have been instantiated. A<br />

Shared class variable represents class-wide information—all class objects share the same<br />

piece of data. The declaration of a Shared member begins with the keyword Shared.<br />

In <strong>Visual</strong> <strong>Basic</strong>, programmers can define what is known as a shared construc<strong>to</strong>r, which<br />

is used only <strong>to</strong> initialize Shared class members. Shared construc<strong>to</strong>rs are optional and<br />

must be declared with the Shared keyword. Normally, Shared construc<strong>to</strong>rs are used<br />

when it is necessary <strong>to</strong> initialize a Shared class variable before any objects of that class<br />

are instantiated. Shared construc<strong>to</strong>rs are called before any Shared class members are<br />

used and before any class objects are instantiated.<br />

We now employ a video-game example <strong>to</strong> explain the need for Shared class-wide data.<br />

Suppose we have a video game in which CMartians attack with other space creatures. Each<br />

CMartian tends <strong>to</strong> be brave and willing <strong>to</strong> attack other space creatures when the CMartian<br />

is aware that there are at least four other CMartians present. If there are fewer than<br />

a <strong>to</strong>tal of five CMartians present, each CMartian becomes cowardly. For this reason,<br />

each CMartian must know the martianCount. We could endow class CMartian with<br />

martianCount as instance data. If we were <strong>to</strong> do this, then every CMartian would have<br />

a separate copy of the instance data, and, every time we create a CMartian, we would have<br />

<strong>to</strong> update the instance variable martianCount in every CMartian. The redundant copies<br />

waste space, and the updating of those copies is time-consuming. Instead, we declare martianCount<br />

<strong>to</strong> be Shared so that martianCount is class-wide data. Each CMartian<br />

can see the martianCount as if it were instance data of that CMartian, but <strong>Visual</strong> <strong>Basic</strong><br />

maintains only one copy of the Shared martianCount <strong>to</strong> save space. We also save time,<br />

in that the CMartian construc<strong>to</strong>r increments only the Shared martianCount. Because<br />

there is only one copy, we do not have <strong>to</strong> increment separate copies of martianCount for<br />

each CMartian object.<br />

Performance Tip 8.2<br />

When a single copy of the data will suffice, use Shared class variables <strong>to</strong> save s<strong>to</strong>rage. 8.2<br />

Although Shared class variables might seem like global variables in C and C++<br />

(variables that can be referenced directly by name in any C function or C++ class or method<br />

in a program), they are not the same thing. Shared class variables have class scope. A<br />

class’s Public Shared members can be accessed through the class name using the dot<br />

opera<strong>to</strong>r (e.g., className.sharedMemberName). A class’s Private Shared class<br />

members can be accessed only through methods of the class. Shared class members are<br />

available as soon as the class is loaded in<strong>to</strong> memory at execution time; like other variables

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

Saved successfully!

Ooh no, something went wrong!