Learn to program in Visual Basic .NET, the right way

Methods - Parameters

What is a Parameter?

Parameters are special types of variables that allow programmers to use data/values inside of methods.

A terminology associated with parameters is the word 'argument'. An argument is the data/value that the programmer passed to the method, where as the parameter is the variable representation of that argument.

There are two types of parameters:

  • ByRef
  • ByVal

Parameters are declared in the parenthesis after a method's name much like how a variable is declared, only instead of a modifier being used, ByRef or ByVal is used.

ByRef

A ByRef parameters represents a value that is referenced outside the method. Any changes made to the parameter inside the method will also persist outside the method.

The following demonstrates how to create a ByRef parameter:

Fiddle: Live Demo

ByVal

A ByVal parameters represents a value that is only referenced inside the method. Any changes made to the parameter inside the method will not persist outside the method.

The following demonstrates how to create a ByVal parameter:

Fiddle: Live Demo

Remarks

If you omit whether or not the parameter is ByRef or ByVal, then it is assumed that the parameter is a ByVal parameter.

With ByRef parameters being able to change values outside of the method, it is important when to consider a ByRef parameter versus a Function. It is a better solution to use a Function in the scenario that you have a Sub Routine with a single ByRef parameter.