' Visual Basic 2008 9.0 .NET Examples - Parameter Arrays - ParamArray - Procedures and Functions : 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()) ' (1) ' 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 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)) ' (1) ' Console.ReadLine() End Sub End Module 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 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 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 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 ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.