|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| When a methods needs to work with an arbitrary number of argumentvalues of a specific type, a parameterarray ( keyword ParamArray ) with element of that type can be used.
In Example1 function GetSum must deliver the sum of an arbitrary number of Integer values.
Paramarray values will hold a reference of an Integer array containing the provided values. |
| Module Example1
Sub Main()
Console.WriteLine(GetSum(1, 2, 3, 4, 5))
Console.WriteLine(GetSum(1, 2, 3, 4))
Console.WriteLine(GetSum(1, 2, 3))
Console.WriteLine(GetSum(1, 2))
Console.WriteLine(GetSum(1))
Console.WriteLine(GetSum())
Console.ReadLine()
End Sub
Function GetSum(ByVal ParamArray values As Integer()) As Integer
If values IsNot Nothing Then
For Each value As Integer In values
GetSum += value
Next
End If
End Function
End Module Download Broncode |
| The implementation of GetSum can use values as any other array would be used.
It is possible to provide no values for a parameterarray (1). In that case will the ParamArray variable hold a reference to an empty ( without elements ) arrayinstance. The selection If values IsNot Nothing Then is not needed in this situation.
ByRef ParamArray is an illegal combination. One can only work "by value" ( ByVal ) with the provides values.
There can be only one parameterarray in an argumentslist, and this 'ParamArray' must be at the end of the parameterslist. If not, the compiler couldn't know which values need to be assigned to which parameters.
ParamArray can not be combined with optional parameters. |
| Module Example2
Sub Main()
Dim values As Integer() = {1, 2, 3, 4, 5}
Console.WriteLine(Example1.GetSum(values))
values = Nothing
Console.WriteLine(Example1.GetSum(values))
Console.ReadLine()
End Sub
End Module Download Broncode |
| As the above example illustrates a parameterarray can work with a provided arrayreference. For this purpose the ParamArray keyword was not necessary.
It is however syntactily correct to assign Nothing to an arrayvariable, therefore it is possible to provide Nothing to an parameterarray. In that case a condition like values IsNot Nothing can be usefull.
Example1.GetSum will only iterate over the elements of values when values is not Nothing, otherwise the For Each would trigger an exception. |
Exercises
| Create a module Exercise1Solution with function GetConcatenation. |
| Module Exercise1Task
Sub Main()
Console.WriteLine(Exercise1Solution.GetConcatenation("abc", "def", _
"ghi"))
Console.WriteLine(Exercise1Solution.GetConcatenation("abc", "def"))
Console.WriteLine(Exercise1Solution.GetConcatenation("abc"))
Console.WriteLine(Exercise1Solution.GetConcatenation())
Dim words As String() = {"abc", "def", "ghi"}
Console.WriteLine(Exercise1Solution.GetConcatenation(words))
words = Nothing
Console.WriteLine(Exercise1Solution.GetConcatenation(words))
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : abcdefghi
abcdef
abc
abcdefghi |
| Module Exercise1Solution
Function GetConcatenation(ByVal ParamArray words As String()) As String
If words IsNot Nothing Then
For Each word As String In words
GetConcatenation &= word
Next
End If
End Function
End Module Download Broncode |
| Create module Exercise2Solution with the methods GetArrayCopy and MultiplyIntegerValues. |
| Module Exercise2Task
Sub Main()
Dim values As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Example1.PrintArray(values)
Dim count As Integer = 5
Dim valuesCopy As Integer()
valuesCopy = Exercise2Solution.GetIntegerArrayCopy(values, count)
Example1.PrintArray(valuesCopy)
Dim factor As Integer = 4
count = 3
Exercise2Solution.MultiplyIntegerValues(valuesCopy, count, factor)
Example1.PrintArray(values)
Example1.PrintArray(valuesCopy)
count = 6
Exercise2Solution.MultiplyIntegerValues(values, count)
Example1.PrintArray(values)
Example1.PrintArray(valuesCopy)
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
4 8 12 4 5
2 4 6 8 10 12 7 8 9 10
4 8 12 4 5 |
| Module Exercise2Solution
Function GetIntegerArrayCopy(ByVal values As Integer(), _
ByVal count As Integer) As Integer()
Dim valuesCopy(count - 1) As Integer
For index As Integer = 0 To count - 1
valuesCopy(index) = values(index)
Next
GetIntegerArrayCopy = valuesCopy
End Function
Sub MultiplyIntegerValues(ByVal values As Integer(), _
ByVal count As Integer, _
Optional ByVal factor As Integer = 2)
For index As Integer = 0 To count - 1
values(index) *= factor
Next
End Sub
End Module Download Broncode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|