Parameters and Arguments VB.NET

The terms parameter and argument are often used interchangeably, although they have entirely different meanings. Let us illustrate with an example. Consider the following function, which replicates a string a given number of times:

The variables sInput and iCount are the parameters of this function. Note that each parameter has an associated data type.

Now, when we call this function, we must replace the parameters by variables, constants, or literals,as in:

The items that we use in place of the parameters are called arguments.

Passing Arguments

Arguments can be passed to a function in one of two ways: by value or by reference. Incidentally, argument passing is often called parameter passing, although it is the arguments and not the parameters that are being passed.

The declaration of RepeatString given earlier contains the keyword ByVal in front of each parameter. This specifies that arguments are passed by value to this function. Passing by value means that the actual value of the argument is passed to the function. This is relevant when an argument is a variable. For instance, consider the following code:

The final line:

actually displays the number 20. In other words, the line:

does nothing. The reason is that the argument iAge is passed to the procedure Inc by value. Since only the value (in this case 20) is passed, that value is assigned to a local variable named x within the procedure. This local variable is increased to 21, but once the procedure ends, the local variable is destroyed. The variable iAge is not passed to the procedure, so its value is not changed.

On the other hand, if we modify the definition of the procedure Inc, replacing ByVal with ByRef, the story is different:

In this case, what is passed to the procedure Inc is a reference to the argument iAge. Hence, the procedure actually operates on the variable passed to it, incrementing the value of iAge to 21. Put another way, the variable represented by the parameter x is actually the passed variable iAge. In VB .NET, the default method of argument passing for arguments is by value. This is a change from earlier versions of VB, in which the default method was by reference.

Passing Objects

There is a subtlety in argument passing with parameters of any object type. Actually, the subtlety occurs because an object variable is a pointer ; that is, it contains a reference to (or the address of) the object.

If we pass an object variable by value, we are passing the contents of the variable, which is the address of the object. Thus, any changes made in the called procedure affects the object itself, not a copy of the object. This seems like passing by reference, but it is not. Think of it this way: passing the value of an object's address is passing a reference to the object.

On the other hand, if we pass an object variable by reference, we are passing the address of the variable. In other words, we are passing the address of the address of the object! In languages that support pointers, this is referred to as a double pointer.

Let us illustrate with an example. Consider the following code, and imagine that the form containing this code has two textboxes: TextBox1 with text "TextBox1" and TextBox2 with text "TextBox2":

Now, here is what happens when we execute DoIt. Note that the argument is passed to GetText by value in this case.

  • The TextBox variable t is assigned to TextBox1.
  • Assigning an object reference
    Assigning an object reference

  • GetText is called, passing t by value. Since t contains the address aaaa of the TextBox1 object, the local variable txt is given the value aaaa.
  • Passing an object by value
    Passing an object by value

  • The single line of code in GetText is executed, which now causes txt to point to TextBox2.
  • Assigning a new object reference
    Assigning a new object reference
  • Upon return from GetText, t is unaffected, so the MsgBox function displays the string"TextBox1."
  • Now suppose we change the ByVal keyword to ByRef in GetText. Here is what happens:

  • The TextBox variable t is assigned to TextBox1, as shown previously.
  • GetText is called, passing t by reference. Hence, txt is t. This is quite different from txt and t containing the same value, as in the ByVal case. The situation is shown i
  • Passing an object by reference
    Passing an object by reference
  • The single line of code in GetText is executed, which now causes txt (and hence t) to point to TextBox2.
  • Assigning a new object reference
    Assigning a new object reference
  • Upon return from GetText, t is now pointing to TextBox2, so the MsgBox function displays the string "TextBox2."

Optional Arguments

In VB .NET, parameters can be declared as optional using the Optional keyword, as shown in the following code:

In VB .NET, all optional parameters must declare a default value, which is passed to the procedure if the calling program does not supply that parameter.

The following rules apply to optional arguments:

  • Every optional argument must specify a default value, and this default must be a constantexpression (not a variable).
  • Every argument following an optional argument must also be optional.

Note that in earlier versions of VB, you could omit the default value and, if the parameter was of type Variant, you could use the IsMissing function to determine if a value was supplied. This is not possible in VB .NET, and the IsMissing function is not supported.

ParamArray

Normally, a procedure definition specifies a fixed number of parameters. However, the ParamArray keyword, which is short for Parameter Array, permits us to declare a procedure with an unspecified number of parameters. Therefore, each call to the procedure can use a different number of parameters. Suppose, for instance, that we want to define a function to take the average of a number of test scores, but the number of scores may vary. Then we declare the function as follows:

Now we can make calls to this function with a varying number of arguments

The following rules apply to the use of ParamArray:

  • A procedure can only have one parameter array, and it must be the last parameter in theprocedure.
  • The parameter array must be passed by value, and you must explicitly include ByVal in the procedure definition.
  • The parameter array must be a one-dimensional array. If the type is not declared, it is assumed to be Object.
  • The parameter array is automatically optional. Its default value is an empty one-dimensional array of the parameter array's data type.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics