Parameters

 <  Day Day Up  >  

Callers can pass values into methods through parameters . Parameters are declared in parentheses after the name of a method. Each parameter is declared like a local variable, with a name and, optionally , a type supplied. Multiple parameters are separated by commas. The following examples show functions declared with parameters.

 Function Square(ByVal Number As Integer) As Integer   Return Number * Number End Function Function Multiply(ByVal x As Integer, ByVal y As Integer) As Integer   Return x * y End Function 

Style

A method declaration with no parameters can choose to omit the parentheses, but they are suggested for emphasis and readability.


There are two types of parameters: value parameters and reference parameters. A value parameter is equivalent to a local variable initialized by the caller. Parameters are value parameters by default; prefixing a parameter with the keyword ByVal explicitly makes the parameter a value parameter.

Compatibility

In previous versions of Visual Basic, parameters were reference parameters by default. The default changed in Visual Basic .NET because the overhead of reference parameters made them less desirable as the default type of parameter.


A reference parameter is special in that it is not a variable at all ”instead, it is a reference to the variable that was passed in by the caller. Reference parameters are declared by prefixing the parameter with the keyword ByRef . When a value is read from or assigned to a reference parameter, the program actually reads or assigns the value that is contained in the variable that was passed in by the caller. Thus, the caller will see any changes made in the variable when the function returns, and vice versa. For example:

 Function Multiply(ByVal x As Integer, ByVal y As Integer, _            ByRef OperationOverflowed As Boolean) As Integer   Try     Multiply = x * y   Catch e As OverflowException     OperationOverflowed = True   End Try End Function Sub Main()   Dim Overflowed As Boolean   Dim Result As Integer   Result = Multiply(10, 20, Overflowed)   If Overflowed Then     Console.WriteLine("Overflow")   Else     Console.WriteLine(Result)   End If End Sub 

In this example, the parameter OperationOverflowed is a reference parameter. Instead of just holding the value passed in from the variable Overflow in the subroutine Main , it represents the variable Overflow itself. So when a value is assigned to the parameter OperationOverflowed , the value is actually stored in the variable Overflow . In all other regards, though, OperationOverflowed looks like a normal variable. It's just that its storage is provided by the caller. Reference parameters are mainly used when a method needs to return more than one value.

Optional Parameters and Parameter Arrays

Sometimes a parameter to a method will have a common value that most callers will use. For example, the following method has a parameter, NewLine , that will usually be passed the value False .

 Sub PrintList(ByVal Number As Integer, ByVal NewLine as Boolean)   If NewLine Then     Console.WriteLine(Number)   Else     Console.Write(Number & ",")   End If End Sub Sub Main()   PrintList(10, False)   PrintList(20, False)   PrintList(30, False)   PrintList(40, True) End Sub 

In this situation, it would be convenient to have the PrintList method assume that the value of the parameter NewLine is False unless the caller said otherwise .

Optional parameters allow specifying the default value for the parameter if the caller does not specify a value explicitly. Optional parameters are parameters with the Optional keyword in front of them and a default value assigned to them after the declaration (just like a local variable initializer). Optional parameters must always be declared after nonoptional parameters. The preceding example can be rewritten as follows , making the NewLine parameter optional, with a default value of False . Thus, only the method call that wishes to use a different value for the parameter must specify it.

 Sub PrintList(ByVal Number As Integer, _           Optional ByVal NewLine as Boolean = False)   If NewLine Then     Console.WriteLine(Number)   Else     Console.Write(Number & ",")   End If End Sub Sub Main()   PrintList(10)   PrintList(20)   PrintList(30)   PrintList(40, True) End Sub 

The default value of an optional parameter must be a constant value. This is because the optional value is substituted for the omitted argument at compile time rather than runtime.

Compatibility

Previous versions of Visual Basic supported optional Variant parameters without default values. Visual Basic .NET no longer supports declaring such parameters, but does still support calling COM methods that have such parameters.


There are also situations where a method can take a variable number of parameters. In the PrintList example used earlier, it would be more convenient if the caller could supply one or more of the numbers to print in a single call. The problem, of course, is that it is not always possible to know ahead of time how many parameters the method might be called with. Parameter arrays can be used instead to represent a variable number of parameters to a method.

A parameter array is declared by putting the ParamArray keyword in front of a parameter typed as a one-dimensional array. The parameter array must be the last parameter in the parameter list. When the method is called, any extra arguments at the end of the argument that do not match other parameters to the method are collected into an array, and the array is passed into the parameter array. The type of the array specifies what type each extra parameter will be converted to. For example, PrintList could be rewritten to take a parameter array of Integer values.

 Sub PrintList(ByVal Header As String, _               ByVal ParamArray Numbers() As Integer)   Console.Write(Header & ": ")   For Index As Integer = 0 To Numbers.Length - 2     Console.Write(Numbers(Index) & ",")   Next Index   Console.WriteLine(Numbers(Numbers.Length - 1)) End Sub Sub Main()   PrintList("Numbers", 10, 20, 30, 40) End Sub 

In this example, the first parameter to PrintList , the string " Numbers ," is matched to the parameter Header . All the rest of the numbers are gathered into an Integer array and passed to the method as a whole.

It is also possible to pass an array to a parameter array, in which case the array represents the array that would have been passed to the parameter by the compiler. This is mostly useful when one method with a parameter array wishes to call another method that takes a parameter array.

 Sub PrintNumberList(ByVal ParamArray Numbers() As Integer)   For Index As Integer = 0 To Numbers.Length - 2     Console.Write(Numbers(Index) & ",")   Next Index   Console.WriteLine(Numbers(Numbers.Length - 1)) End Sub Sub PrintList(ByVal Header As String, _           ByVal ParamArray Numbers() As Integer)   Console.Write(Header & ": ")   PrintNumberList(Numbers) End Sub Sub Main()   PrintList("Numbers", 10, 20, 30, 40) End Sub 

In this example, the method PrintList passes the parameter array Numbers to the Numbers parameter of the PrintNumberList method as a whole array.

NOTE

Not all .NET Framework languages support optional parameters. Languages such as Visual C# .NET require all parameters to be explicitly supplied, even if they are declared as optional in Visual Basic .NET.


 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

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