H o m e

 

I m p l e m e n t i n g    C l a s s    M e t h o d s    

By Michael McIntyre

mikemc@getdotnetcode.com

 

This article discusses the basics of implementing class methods in a class statement. The majority of the code a Visual Basic.Net programmer writes is in the form of class methods. Class methods define which operations can be performed directly through the class (Shared methods) or on objects created from the class. 

In Visual Basic.Net the methods of a class statement are the Sub and Function procedures declared within the class statement. For example, here is a class statement for a class named ‘Account’ which defines two methods.

Public Class Account

 

#Region "Fields"

    ' AccountHolderName field.

    Private _AccountHolderName As String

    ' Balance field.

    Private _Balance As Decimal

#End Region

 

#Region "Properties"

    ' AccountHolderName property.

    Public Property AccountHolderName() As String

        Get

            Return _AccountHolderName

        End Get

        Set(ByVal Value As String)

            _AccountHolderName = Value

        End Set

    End Property

 

    ' Balance property.

    Public Property Balance() As Decimal

        Get

            Return _Balance

        End Get

        Set(ByVal Value As Decimal)

            _Balance = Value

        End Set

    End Property

#End Region

 

#Region "Public Methods"

    ' SetAccountHolderName Method.

    ' Does NOT return a value when called.

    Public Sub SetAccountHolderName(ByVal accountHolderName As String)

        Me.AccountHolderName = accountHolderName

    End Sub

 

    ' PayInterest Method.

    ' Returns balance after interest is paid.

    Public Function PayInterest(ByVal interestRate As Decimal) As Decimal

        Me.Balance = Me.Balance + (Me.Balance * interestRate)

        Return Me.Balance

    End Function

#End Region

 

End Class

NOTE: While Sub and Function procedures are public by default, it is good object-oriented programming practice that you explicitly specify the Public keyword when you declare a public method. This is done by preceeding the Sub or Function keyword with the Public keyword. (See the Sub and Function declarations above.)

Shared Methods

Shared methods can be invoked directly from a class 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.  See the System.Math class; it contains many shared methods. Here is code that uses the Math class’ shared ‘Abs’ method.  Note that it was not necessary to use the ‘New’ keyword to instantiate an instance of the Math class to use its shared methods.

MessageBox.Show(System.Math.Abs(-1).ToString)

To define a class method as Shared, the keyword Shared must be included in the method declaration. For example, here is a class named MyUtilities that declares two shared methods.

Public Class MyUtilities

 

#Region "Public Shared Methods"

    ' CurrentComputerUser Shared Method.

    Public Shared Function CurrentComputerUser() As String

        Return System.Environment.UserName

    End Function

 

    ' DefaultPrinter Shared Method

    Public Shared Function DefaultPrinter() As String

        Dim typePrinterDocument As New System.Drawing.Printing.PrintDocument()

        Return typePrinterDocument.DefaultPageSettings.PrinterSettings.PrinterName

    End Function

#End Region

 

End Class

Your code need only use the class name to use the class’ shared methods. To call a shared method from a class type the class name, followed by a period, followed by the shared method to be called.  Here is example code that calls the MyUtilities ‘CurrentComputerUser’ method directly through the MyUtilities class name.

 

MessageBox.Show(MyUtilities.CurrentComputerUser)

Protecting Class Implementation Details

Protecting class implementation details is a facet of encapsulation.

The public interface of a class is the Public property and method declarations in the class statement that defines the class. Public methods are the methods that will be seen through Intellisense when using a class in code. You can change the code inside the Public properties and methods, without affecting code that uses the method.  As long as you don't change the data types of the public method's arguments, or the type of data returned if the method is a Function procedure, the public interface is unchanged. Thus, Public properties and methods are known only by their name, arguments, and return type to the code that uses them.

Methods you declare as Private are not part of the public interface; they can not be called from code using the class. Private methods will not be seen through Intellisense when using the class in code. Private methods are encapsulated within the class; they are not exposed to code using the class This means that you can make changes to methods that are used internally by the class without affecting client code that uses the class.

Click here to learn more about Visual Basic.NET class method statements.

NOTE: Portions of this article are parts and pieces of Microsoft’s MSDN’s Visual Basic 6 documentation updated to reflect how class methods are implemented with Visual Basic.NET.

 

Copyright © 2001-2005 aZ Software Developers. All rights reserved.