Visual Basic Methods (Continued, Optional Parameters and Overloading)

[This tutorial continues from a previous tutorial, VB Methods ]

Optional Parameters

Optional parameters in methods are available, and  to specify a parameter as optional you use the optional keyword, followed by parameter name,  equal sign and value. Change the VAT method so it looks like this:

Public Function VAT(ByVal pCost As Double, Optional cRate As Double = 20)
        
cRate = (cRate + 100) / 100
Return pCost * cRate

End Function

As you can see, cRate is now optional and has a default value of 20. All optional values must go at the end.

In the Sub Main change the code to:

   Console.Write("Please Enter product cost:")

        Dim PC As String = Console.ReadLine()
        ' Console.Write("Please enter current VAT rate:")
        ' Dim cR As String = Console.ReadLine()

        Dim result As Double = VAT(Convert.ToDouble(PC))
        Console.WriteLine("{0:C}", result)
        Console.ReadLine()

Here you do not need to specify the second parameter as it has a default value of 20.

Overloading Methods

Overloading methods can be extremely useful; for example you might want to perform multiple functions on the same method but just change the parameters. To specify that a method is overloaded you create multiple methods with the same name but different parameters:

Public Function Games(ByVal newGames As String())

End Function

Public Function Games(ByVal newGames As String)

End Function

The parameters must be different; this, for example, would not work:

Public Function Games(ByVal newGames As String)

End Function


Public Function Games(ByVal newGames As String)

End Function

VB would not know which method to work with. Additionally, something like this also would not work:

Public Function VAT(ByVal pCost As Double)

End Function

  
Public Function VAT(ByVal pCost As Integer)
     
End Function

Although the data types are different the user can still put something like VAT(20). Which method should VB work with? Doubles can contain integer values.

Example – Overloaded Method

Public Function Games(ByVal newGames As String())
        For Each g In newGames
            Console.WriteLine(g)
        Next

        Return Nothing

    End Function

Method 2

Public Function Games(ByVal newGames As String)

        Console.WriteLine(newGames)

        Return Nothing

    End Function

These two methods are different; one accepts an array of values and one simply accepts a single value.

In Sub Main put:

Dim myGames As String() = {"GTA 5", "BF4", "GRID2"}

Console.WriteLine(Games(myGames))
Console.ReadLine()

Or a single value:

Console.WriteLine(Games(“GRID 2”))
Console.ReadLine()

When you start typing, VS will show a tool-tip telling you what arguments the method accepts.

Description

When you use the Console.WriteLine() method it gives a small description of the method and the parameters, and you can easily add that to your own methods. To add it simply put 3 apostrophes above the method, and VS will automatically insert the syntax.

Example

  ''' <summary>
    ''' Prints out a single list of games
    ''' </summary>
    ''' <param name="newGames">String Games, What game do you like?</param>
    ''' <returns></returns>
    ''' <remarks></remarks>

    Public Function Games(ByVal newGames As String)

        Console.WriteLine(newGames)
        Return Nothing

    End Function

When you call the method now, VS will automatically provide the description. (A tooltip)

Summary

  • Sub means subroutine
  • Sub does not return a value, function does
  • When giving information to methods it is known as parameters
  • Variables in methods are local
  • You call a method by its name