
OOP Development in Visual Basic | 35
OOP
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Instance Members Versus Shared Members
Members of a class can either be instance members or shared members. Instance
members are only useful in a specific instance of the class, that is, from an object.
Until an instance of the object exists, these members cannot be used or refer-
enced in any way. Instance members belong to specific instances of the class
instead of to the class as a whole. The members added to the sample
Person class
above are all instance members.
Public Class SimpleClass
' ----- This is an instance member.
Public Comment As String
End Class
...
' ----- In some other code.
Dim myInstnace = New SimpleClass
myInstance.Comment = "I am not shared!"
Shared members (sometimes called static members) can be accessed without the
presence of any particular instance of the class. They belong to the whole class,
but they are also “shared” among all instances of the class. Shared members are
accessed by qualifying the name of the member with the name of the class.
Public Class SimpleClass
' ----- This is an instance member.
Public Shared Comment As String
End Class
...
' ----- In some other code.
SimpleClass.Comment = "I am shared!"
All members of a Module are automatically shared, even though the Shared
keyword is not used on each member of the ...