This documentation is archived and is not being maintained.

Method Changes for Visual Basic 6.0 Users 

A method is a Sub or Function procedure declaration. Methods make a class do things. Together with properties, they expose the behavior and data of a class.

Method Declarations

Visual Basic 6.0

In Visual Basic 6.0 class definitions can contain Sub and Function methods. Those methods can be Public, Private, or Friend. Here are two examples:

Public Sub Draw(theForm As Form)
    ' Add code here to draw a tree on the form.
End Sub

Public Function GrowOneYear() As Integer
    mvarHeight = mvarHeight + 10
    GrowOneYear = mvarHeight
End Function

Visual Basic 2005

Visual Basic 2005 also supports Sub and Function methods that are Public, Private, and Friend. Either ByRef or ByVal is required on all method parameters. If not explicitly added, ByVal is added by the Code Editor.

Code upgraded from Visual Basic 6.0 will be upgraded to ByRef, as this was the default in Visual Basic 6.0:

Public Sub Draw(ByRef theForm As System.Windows.Forms.Form)
    ' Add code here to draw a tree on the form.
End Sub

Method Overloading

Visual Basic 6.0

In Visual Basic 6.0, a class can only contain one method with a given name.

Visual Basic 2005

In Visual Basic 2005, methods can be overloaded. A method is overloaded when more than one version of the method is defined in the class. The overloaded versions differ in their parameters and returns types. This can simplify the interface of an object by making it smaller. The following example shows two overloads of the Draw method. For more information, see Overloaded Properties and Methods.

Public Sub Draw(ByRef theForm As System.Windows.Forms.Form)
    ' Add code here to draw a tree on the form.
End Sub

Public Sub Draw(ByVal surface As System.Drawing.Graphics)
    ' Add code here to draw a tree on the graphics surface.
End Sub

Return Keyword

Visual Basic 6.0

In Visual Basic 6.0, the function name is used as a variable to return the value of the function.

Public Function GrowOneYear() As Integer
    heightValue = heightValue + 10
    GrowOneYear = heightValue
End Function

Visual Basic 2005

Visual Basic 2005 includes the explicit Return statement for returning function values. The GrowOneYear function in Visual Basic 2005 looks like this:

Public Function GrowOneYear() As Integer
    heightValue += 10
    Return heightValue
End Function

For more information, see Return Statement (Visual Basic).

Upgrade Suggestions

The Upgrade Wizard produces this code from the Draw method:

Public Sub Draw(ByRef theForm As System.Windows.Forms.Form)
    ' Add code here to draw a tree on the form.
End Sub

Some techniques to consider in upgraded methods include:

  • Consider using overloaded methods for methods that have similar names and similar behavior, but vary by parameter and return types.

  • Verify the conversion of method scope (Public, Private, Friend) and return type (Short, Integer, and so forth).

  • Consider using the Return statement instead of the function name to return the function value.

See Also

Show: