Parameter Passing


When an object’s methods or an assembly’s procedures and methods are called, it’s often appropriate to provide input for the data to be operated on by the code. The values are referred to as parameters, and any object can be passed as a parameter to a Function or Sub.

When passing parameters, be aware of whether the parameter is being passed “by value” (ByVal) or “by reference” (ByRef). Passing a parameter by value means that if the value of that variable is changed, then when the Function/Sub returns, the system automatically restores that variable to the value it had before the call. Passing a parameter by reference means that if changes are made to the value of a variable, then these changes affect the actual variable and, therefore, are still present when the variable returns.

This is where it gets a little challenging for new Visual Basic developers. Under .NET, passing a parameter by value indicates only how the top-level reference (the portion of the variable on the stack) for that object is passed. Sometimes referred to as a shallow copy operation, the system copies only the top-level reference value for an object passed by value. This is important to remember because it means that referenced memory is not protected. When you pass an integer by value, if the program changes the value of the integer, then your original value is restored. However, if you pass a reference type, only the location of your referenced memory is protected, not the data located within that memory location. Thus, while the reference passed as part of the parameter remains unchanged for the calling method, the actual values stored in referenced objects can be updated even when an object is passed by value.

In addition to mandatory parameters, which must be passed with a call to a given function, it is possible to declare optional parameters. Optional parameters can be omitted by the calling code. This way, it is possible to call a method such as PadRight, passing either with a single parameter defining the length of the string and using a default of space for the padding character, or with two parameters, the first still defining the length of the string but the second now replacing the default of space with a dash.

  Public Function PadRight(ByVal intSize as Integer, _                          Optional ByVal chrPad as Char = " "c) End Function 

To use default parameters, it is necessary to make them the last parameters in the function declaration. Visual Basic also requires that every optional parameter have a default value. It is not acceptable just to declare a parameter and assign it the Optional keyword. In Visual Basic, the Optional keyword must be accompanied by a value that is assigned if the parameter is not passed in.

ParamArray

In addition to passing explicit parameters, it is also possible to tell .NET that you would like to allow a user to pass any number of parameters of the same type. This is called a parameter array, and it enables a user to pass as many instances of a given parameter as are appropriate. For example, the following code creates a function Add, which allows a user to pass an array of integers and get the sum of these integers:

  Public Function Add(ByVal ParamArray values() As Integer) As Long     Dim result As Long = 0     For Each value As Integer In values         result += value     Next     Return result End Function 

The preceding code illustrates a function (which was first shown at the start of this chapter without its implementation) that accepts an array of integers. Notice that the ParamArray qualifier is preceded by a ByVal qualifier for this parameter. The ParamArray requires that the associated parameters be passed by value and cannot be optional parameters.

You might think this looks like a standard parameter passed by value except that it’s an array, but there is more to it than that. In fact, the power of the ParamArray is how it can be called, which also explains many of its limitations. The following code shows one way this method can be called:

  Dim int1 as Integer = 2 Dim int2 as Integer = 3 Dim sum as Long = Add(1, int1, int2) 

Notice that the preceding line, which calls this Add function, doesn’t pass an array of integers; instead, it passes three distinct integer values. The ParamArray keyword tells Visual Basic to automatically join these three distinct values into an array for use within this method. However, notice that the following lines also represent an acceptable way to call this method, by passing an actual array of values:

  Dim myIntArray() as Integer = {1, 2, 3, 4} Dim sum as Long = Add(myIntArray) 

Finally, note one last limitation on the ParamArray keyword - it can only be used on the last parameter defined for a given method. Because Visual Basic is grabbing an unlimited number of input values to create the array, there is no way to indicate the end of this array, so it must be the final parameter.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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