(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
ParamArray Parameter (Variable Args)
Module Module1
Sub Main()
ShowMessage("Hello there!")
ShowMessage("Hello", " there!")
End Sub
Sub ShowMessage(ByVal ParamArray Text() As String)
Dim i As Integer
For i = 0 To UBound(Text)
System.Console.Write(Text(i))
Next i
End Sub
End Module
Hello there!Hello there!"
Using ParamArray to create variable-length parameter lists.
Module Tester
Sub Main()
AnyNumberArguments()
AnyNumberArguments(2, 3)
AnyNumberArguments(7, 8, 9, 10)
End Sub " Main
Sub AnyNumberArguments(ByVal ParamArray array1 _
As Integer())
Dim i, total As Integer
total = 0
If array1.Length = 0 Then
Console.WriteLine(" received 0 arguments.")
Else
Console.Write("The total of ")
For i = 0 To array1.GetUpperBound(0)
Console.Write(array1(i) & " ")
total += array1(i)
Next
Console.WriteLine("is {0}.", total)
End If
End Sub
End Module
received 0 arguments.
The total of 2 3 is 5.
The total of 7 8 9 10 is 34.
Varied length Method parameter
Option Strict On
Imports System
Class Tester
Public Shared Sub DisplayVals(ByVal ParamArray intVals( ) As Integer)
Dim i As Integer
For Each i In intVals
Console.WriteLine("DisplayVals {0}", i)
Next i
End Sub
Shared Sub Main( )
Dim a As Integer = 5
Dim b As Integer = 6
Dim c As Integer = 7
Console.WriteLine("Calling with three Integers")
DisplayVals(a, b, c)
Console.WriteLine("Calling with four Integers")
DisplayVals(5, 6, 7, 8)
Console.WriteLine("calling with an array of four Integers")
Dim explicitArray( ) As Integer = {5, 6, 7, 8}
DisplayVals(explicitArray)
End Sub
End Class
Calling with three Integers
DisplayVals 5
DisplayVals 6
DisplayVals 7
Calling with four Integers
DisplayVals 5
DisplayVals 6
DisplayVals 7
DisplayVals 8
calling with an array of four Integers
DisplayVals 5
DisplayVals 6
DisplayVals 7
DisplayVals 8