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
|
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
