18.10. Using Shared Members


The properties and methods of a class can be either instance members or Shared members. Instance members are associated with instances of a type, while Shared members are associated with the class, and not with any particular instance.

You may access a Shared member through the name of the class in which it is declared. For example, suppose you have a class named Button and have instantiated objects of that class named btnUpdate and btnDelete.

Suppose that the Button class has an instance method Draw and a Shared method GetButtonCount. The job of Draw is to draw the current button; the job of GetButtonCount is to return the number of buttons currently visible on the form.

You access an instance method through an instance of the class; that is, through an object:

 btnUpdate.SomeMethod(  ) 

You access Shared methods through the class name (rather than through an instance):

 Button.GetButtonCount(  ) 

The Shared method is considered a method of the class rather than of an instance of the class, and you do not need an instance of the class to access the method.

Methods are instance methods unless you explicitly mark them with the keyword Shared.


A common use of Shared member variables is to create utility classes with useful methods and not force clients to create an instance if the class just to access the method.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net