The methods of a class are just Sub or Function procedures declared within the class. For example, to create a
Withdrawal method for a class named
Account, you could add this Public function to the class module:
Public Function WithDrawal(ByVal Amount As Decimal, _ ByVal TransactionCode As Byte) As Double ' Add code here to perform the withdrawal, ' return a transaction code, ' or to raise an overdraft error. End Function
Shared methods can be invoked directly from a class variable without first creating an instance of the class. Shared methods are useful when you do not want a method to be associated with a specific instance of a class. Shared methods cannot be declared using the Overridable, NotOverridable, or MustOverride modifiers. Methods declared in modules are implicitly shared and cannot explicitly use the Shared modifier.
Class ShareClass Shared Sub SharedSub() MessageBox.Show("Shared method.") End Sub End Class Sub Test() ' Create an object variable, but not an instance. Dim S As ShareClass S.SharedSub ' Call the method. End Sub
Protecting Implementation Details
Utility procedures that are used internally by a class should be declared as Private, Protected, or Friend. Restricting the accessibility of such methods prevents them from being used by other developers, and allows you to make future changes without affecting code that uses the objects.
Hiding the details of an object's implementation is another facet of encapsulation. Encapsulation allows you to enhance the performance of methods, or completely change the way a method is implemented, without having to change code that uses the method.