Visual Basic Methods (Continued, Optional Parameters and Overloading)

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

 

Optional Parameters

You can have optional parameters in methods, 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 default value of 20, all optional values have to go at the end.

 

In the Sub Main change the code so it looks like this:

   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()

This time 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 a method is overloaded you create multiple methods with the same name but different parameters like this:

Public Function Games(ByVal newGames As String())

End Function

Public Function Games(ByVal newGames As String)

End Function

The parameters must be different this 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. Also 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) now 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 this;

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 want arguments the method accepts.

 

Description

If you noticed when you use the Console.WriteLine() method it gives a small description of the method and the parameters, you can easily add that to your own methods, to add it simply put 3 apostrophe’s above the method, VS will automatically insert the syntax then.

 

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

Modified On: 08 June 2014 - 06:14:22 PM

Advertisement