Vb.net – How to add an optional parameters/default value parameters in VB function


How can I create a method that has optional parameters in it in Visual Basic?

Best Solution

Use the Optional keyword and supply a default value. Optional parameters must be the last parameters defined, to avoid creating ambiguous functions.

Sub MyMethod(ByVal Param1 As String, Optional ByVal FlagArgument As Boolean = True)
    If FlagArgument Then
        'Do something special
    End If

End Sub

Call it like this:


Or like this:

MyMethod("test2", False)