9.10. Shared Class Members

Each object of a class has its own copy of all the instance variables of the class. However, in certain cases, all class objects should share only one copy of a particular variable. A program contains only one copy of a Shared class variable in memory, no matter how many objects of the class have been instantiated. A Shared class variable represents class-wide information—all class objects share the same piece of data. The declaration of a Shared member begins with the keyword Shared.

Let us use a video game example to explain the need for Shared class-wide data. Suppose we have a video game in which Martians attack other space creatures. A Martian tends to be brave and willing to attack other space creatures when it ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.