O'Reilly logo

Object-Oriented Programming with Visual Basic .NET by J.P. Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.3. Methods

In VB.NET, methods come in three varieties: functions, subroutines, and properties.

Functions allow you to return a value resulting from an operation. They are defined with the Function keyword and can contain any number of parameters. They can also return a result to the caller with the Return keyword:

Public Class SpaceTime
   
    'In meters per second
    Private Const LightSpeed As Double = 299792458
    
    Private Function Energy(mass As Double) As Double
       'Same as LightSpeed * LightSpeed * mass
        Return LightSpeed *= LightSpeed * mass
				End Function
   
    'Really technical stuff goes here
End Class

Subroutines perform tasks that do not require a return value and are declared just like functions, except they are defined using the Sub keyword:

Public Class SpaceTime
   
    Public Sub FoldSpace( )
        'Suprisingly simple
    End Sub
    
End Class

A class should represent one and only one abstraction. The methods of a class should use most of the member data within the class most of the time. This means that if you find that half of the class methods operate on half of the data members, but the other methods use the remaining half, there is an implied division within the class. You probably have two classes instead of one.

To formulate this concept as a general principle, make sure there are no broken lines of communication within a class.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required