Section 3.7. Parameters and Arguments

   

3.7 Parameters and Arguments

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:

 Function RepeatString(ByVal   sInput   As String, ByVal   iCount   As Integer) _                       As String     Dim i As Integer     For i = 1 To   iCount   RepeatString = RepeatString &   sInput   Next End Function 

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:

 s = RepeatString("Donna", 4) 

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

3.7.1 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:

 Sub Inc(ByVal   x   As Integer)   x   = x + 1 End Sub Dim   iAge   As Integer = 20 Inc(   iAge   ) Msgbox(   iAge   ) 

The final line:

 Msgbox(   iAge   ) 

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

 Inc(   iAge   ) 

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:

 Sub Inc(ByRef x As Integer)     x = x + 1 End Sub 

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.

3.7.2 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":

 Public Function GetText(ByVal   txt   As TextBox) As String     ' Change reference to textbox   txt   = Textbox2 End Function Sub Doit     Dim t As TextBox   t   = TextBox1     GetText(   t   )     msgbox(t.Text)   ' Displays TextBox1 when ByVal, _                      ' TextBox2 when ByRef End Sub 

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, as shown in Figure 3-3.

    Figure 3-3. Assigning an object reference
    figs/vnl2_0303.gif
  • 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, as shown in Figure 3-4.

    Figure 3-4. Passing an object by value
    figs/vnl2_0304.gif
  • The single line of code in GetText is executed, which now causes txt to point to TextBox2, as shown in Figure 3-5.

    Figure 3-5. Assigning a new object reference
    figs/vnl2_0305.gif
  • 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 in Figure 3-3.

  • 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 in Figure 3-6.

    Figure 3-6. Passing an object by reference
    figs/vnl2_0306.gif
  • The single line of code in GetText is executed, which now causes txt (and hence t ) to point to TextBox2, as shown in Figure 3-7.

    Figure 3-7. Assigning a new object reference
    figs/vnl2_0307.gif
  • Upon return from GetText, t is now pointing to TextBox2, so the MsgBox function displays the string "TextBox2."

3.7.3 Optional Arguments

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

 Sub Calculate(Optional ByVal Switch As Boolean = False) 

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 constant expression (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.

3.7.4 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 :

 Function GetAverage(ByVal ParamArray Scores(  ) As Single) As Single     Dim i As Integer     For i = 0 To UBound(Scores)         GetAverage = GetAverage + CSng(Scores(i))     Next     GetAverage = GetAverage / (UBound(Scores) + 1) End Function 

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

 Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3)) 

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 the procedure.

  • 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.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net