Visual Basic Named Parameters

In visual basic, the named parameters have been introduced to pass the method arguments with parameter name rather than with the method parameter’s position in the parameter list.

 

Generally, while calling the method we need to pass all the method arguments in the same sequence of parameters in the method definition. If we use named parameters, we don’t need to worry about the order or sequence of parameters while calling the method.

 

In named parameters, the parameter values will be mapped to the right parameter based on the parameter name. So, while calling the methods we can send the parameters in any sequence or order if we specify the parameter name.

 

For example, we have a GetDetails() method with three parameters (id, name, location) like as shown below.

 

Public Sub GetDetails(ByVal id As Integer, ByVal name As String, ByVal location As String)

' your code

End Sub

In the standard way, we will call the above method by sending the arguments in the same order or position however we defined the parameters in the method definition like as shown below.

 

GetDetails(1, "Trishi", "Guntur");

In case, if we forgot and send the parameters in different order like as shown following we will get the compile-time error like “The best overloaded method for 'GetDetails(int, string, string)' has some invalid arguments”.

 

GetDetails("Trishi", 1, "Guntur");

By using named parameters, we call the above method by sending the parameters in any sequence or order like as shown below.

 

GetDetails(id: 1, name: "Trishi", location: "Guntur");

GetDetails(name: "Trishi", id: 1, location: "Guntur");

GetDetails(location: "Guntur", name: "Trishi", id: 1);

We can also use the named arguments with positional arguments as long as they are not followed by any positional arguments like as shown below.

 

GetDetails(1, "Trishi", location: "Guntur");

Following is the invalid way of using named and positional arguments while calling the method.

 

GetDetails(name: "Trishi", 1, location: "Guntur");

The above method call will throw the compile-time exception like “Named argument 'name' is used out-of-position but is followed by an unnamed argument” because we defined the positional argument followed by named argument (name).

Visual Basic Named Parameters Example

Following is the example of calling the method by sending the parameters in different sequences using named parameters in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        GetDetails(1, "Suresh", "Hyderabad")

        GetDetails(name:="Rohini", id:=2, location:="Guntur")

        GetDetails(3, "Trishi", location:="Guntur")

        Console.ReadLine()

    End Sub

    Public Sub GetDetails(ByVal id As Integer, ByVal name As String, ByVal location As String)

        Console.WriteLine("Id:{0}", id)

        Console.WriteLine("Name:{0}", name)

        Console.WriteLine("Location:{0}", location)

    End Sub

End Module

If you observe the above example, we are calling the GetDetails() method by sending the parameters in different positions with named parameters.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

****Calling Method Normally****

Id:1

Name:Suresh

Location:Hyderabad

 

****Calling Method with Named Arguments****

Id:2

Name:Rohini

Location:Guntur

 

****Calling Method with Named & Positional Arguments****

Id:3

Name:Trishi

Location:Guntur

This is how we can use the named parameters in visual basic to pass the method arguments with parameter name rather than with the method parameter’s position.

Visual Basic Named Parameters Overview

Following are the important points which we need to remember about named parameters in visual basic.

 

  • The named parameters have been introduced in visual basic to pass the method arguments with parameter name rather than with the method parameter’s position in the parameter list.
  • In named parameters, the parameter values will be mapped to the right parameter based on the parameter name.
  • We can send the parameters in any sequence or order if we specify the parameters name while calling the methods.
  • While calling the method, we can use the named arguments with positional arguments as long as they are not followed by any positional arguments.