This documentation is archived and is not being maintained.

9.2 Methods

Visual Studio .NET 2003

Methods contain the executable statements of a program. Methods, which have an optional list of parameters and an optional return value, are either shared or nonshared. Shared methods are accessed through the class or instances of the class. Nonshared methods, also called instance methods, are accessed through instances of the class. The following example shows a class Stack that has several shared methods (Clone and Flip), and several instance methods (Push, Pop, and ToString):

Imports System

Public Class Stack
    Public Shared Function Clone(ByVal s As Stack) As Stack
    End Function

    Public Shared Function Flip(ByVal s As Stack) As Stack
    End Function

    Public Function Pop() As Object
    End Function

    Public Sub Push(ByVal o As Object)
    End Sub 

    Public Overrides Function ToString() As String
    End Function 
End Class 

Module Test
    Sub Main()
        Dim s As Stack = New Stack()
        Dim i As Integer

        While i < 10
            s.Push(i)
        End While

        Dim flipped As Stack = Stack.Flip(s)
        Dim cloned As Stack = Stack.Clone(s)

        Console.WriteLine("Original stack: " & s.ToString())
        Console.WriteLine("Flipped stack: " & flipped.ToString())
        Console.WriteLine("Cloned stack: " & cloned.ToString())
    End Sub
End Module

Methods can be overloaded, which means that multiple methods may have the same name so long as they have unique signatures. The signature of a method consists of the name of the method and the number and types of its parameters. The signature of a method specifically does not include the return type or parameter modifiers. The following example shows a class with a number of F methods:

Imports System

Module Test
    Sub F()
        Console.WriteLine("F()")
    End Sub 

    Sub F(ByVal o As Object)
        Console.WriteLine("F(Object)")
    End Sub

    Sub F(ByVal value As Integer)
        Console.WriteLine("F(Integer)")
    End Sub 

    Sub F(ByVal a As Integer, ByVal b As Integer)
        Console.WriteLine("F(Integer, Integer)")
    End Sub 

    Sub F(ByVal values() As Integer)
        Console.WriteLine("F(Integer())")
    End Sub 

    Sub Main()
        F()
        F(1)
        F(CType(1, Object))
        F(1, 2)
        F(New Integer() { 1, 2, 3 })
    End Sub
End Module

The output of the program is:

F()
F(Integer)
F(Object)
F(Integer, Integer)
F(Integer())
MethodMemberDeclaration ::= MethodDeclaration | ExternalMethodDeclaration

See Also

9.2.1 Regular Method Declarations | 9.2.2 External Method Declarations | 9.2.3 Overridable Methods | 9.2.4 Shared Methods | 9.2.5 Method Parameters | 9.1 Interface Method Implementation | 9.2.6 Event Handling | 9.3 Constructors | 9.4 Events | 9.5 Constants | 9.6 Instance and Shared Variables | 9.7 Properties | Methods (Visual Basic Language Reference) | Performing Actions with Methods (Visual Basic Language Concepts)

Show: