' Visual Basic 2008 9.0 .NET Examples - Methods and Arguments - Procedures and Functions : Module Example1 Sub Main() Dim value1 As Integer = 5 ShowDouble(value1) ' (1) ' Dim value2 As String = "test" ShowDouble(value2) ' (2) ' Console.ReadLine() End Sub Sub ShowDouble(ByVal value As Integer) ' (1) Console.WriteLine(value * 2) End Sub Sub ShowDouble(ByVal word As String) ' (2) Console.WriteLine(word & word) End Sub End Module Module Example2 Sub Main() Console.WriteLine(GetSum(1, 2)) Console.WriteLine(GetSum(1, 2, 3)) Console.WriteLine(GetSum(1, 2, 3, 4)) ' Console.ReadLine() End Sub Function GetSum(ByVal value1 As Integer, _ ByVal value2 As Integer) As Integer GetSum = value1 + value2 End Function Function GetSum(ByVal value1 As Integer, _ ByVal value2 As Integer, _ ByVal value3 As Integer) As Integer GetSum = value1 + value2 + value3 End Function Function GetSum(ByVal value1 As Integer, _ ByVal value2 As Integer, _ ByVal value3 As Integer, _ ByVal value4 As Integer) As Integer GetSum = value1 + value2 + value3 + value4 End Function End Module Module Exercise1Task Sub Main() 'Console.WriteLine(GetCombination("abc", "def")) 'Console.WriteLine(GetCombination(1, 2)) 'Console.WriteLine(GetCombination(True, True)) 'Console.WriteLine(GetCombination(True, False)) 'Console.WriteLine(GetCombination(False, True)) 'Console.WriteLine(GetCombination(False, False)) ' Console.ReadLine() End Sub End Module Module Exercise1Solution Sub Main() Console.WriteLine(GetCombination("abc", "def")) Console.WriteLine(GetCombination(1, 2)) Console.WriteLine(GetCombination(True, True)) Console.WriteLine(GetCombination(True, False)) Console.WriteLine(GetCombination(False, True)) Console.WriteLine(GetCombination(False, False)) ' Console.ReadLine() End Sub Function GetCombination(ByVal value1 As String, _ ByVal value2 As String) As String GetCombination = value1 & value2 End Function Function GetCombination(ByVal value1 As Integer, _ ByVal value2 As Integer) As Integer GetCombination = value1 + value2 End Function Function GetCombination(ByVal value1 As Boolean, _ ByVal value2 As Boolean) As Boolean GetCombination = value1 AndAlso value2 End Function End Module Module Example3 Sub Main() Console.WriteLine(GetPower(3, 3)) Console.WriteLine(GetPower(3)) ' (1) ' Console.ReadLine() End Sub Function GetPower(ByVal base As Integer, _ Optional ByVal exponent As Integer = 2) As Integer GetPower = base ^ exponent End Function End Module Module Example4 Sub Main() ShowSum(1) ' value1 = 1 | value2 = 2 | value3 = 3 ' (1) ShowSum(2, 3) ' value1 = 2 | value2 = 3 | value3 = 3 ' (1) ShowSum(4, , 5) ' value1 = 4 | value2 = 2 | value3 = 5 ' (1) ShowSum(6, 7, 8) ' value1 = 6 | value2 = 7 | value3 = 8 ' (1) ' ShowSum(value1:=1) ' (2) ShowSum(value1:=2, value2:=3) ' (2) ShowSum(value1:=4, value3:=5) ' (2) ShowSum(value1:=6, value2:=7, value3:=8) ' (2) ' ShowSum(value3:=5, value1:=4) ' (3) ShowSum(value3:=8, value2:=7, value1:=6) ' (3) ' ShowSum(4, value3:=5) ' (4) ' Console.ReadLine() End Sub Sub ShowSum(ByVal value1 As Integer, _ Optional ByVal value2 As Integer = 2, _ Optional ByVal value3 As Integer = 3) Console.WriteLine(value1 + value2 + value3) End Sub End Module Module Example5 Sub Main() Dim numbers As Integer() = {1, 2, 3, 4, 5} PrintArray(numbers) ' Dim words As String() = {"Hello", "World"} PrintArray(words) ' Dim dates As Date() PrintArray(dates) ' (1) ' PrintArray(Nothing) ' Console.ReadLine() End Sub Sub PrintArray(ByVal values As Array) If values IsNot Nothing Then For Each element As Object In values Console.Write(element & " ") Next Console.WriteLine() Else Console.WriteLine("No array.") End If End Sub End Module Module Example6 Sub Main() Dim object1 As Object ' object1 = 5 Console.WriteLine(object1) Console.WriteLine(object1 + 10) ' object1 = "Hello" Console.WriteLine(object1) Console.WriteLine(object1 & " World !") ' object1 = True Console.WriteLine(object1) Console.WriteLine(object1 AndAlso False) ' Console.ReadLine() End Sub End Module Module Example7 Sub Main() Dim values1 As Integer() = {1, 2, 3} Dim values2 As Integer() = {4, 5, 6} Dim values3 As Integer() = values1 Dim values4 As Array = values2 ' Console.WriteLine(values1 IsNot values2) Console.WriteLine(values1 Is values3) Console.WriteLine(values1 IsNot values4) Console.WriteLine() ' Console.WriteLine(values2 IsNot values1) Console.WriteLine(values2 IsNot values3) Console.WriteLine(values2 Is values4) ' Console.ReadLine() End Sub End Module Module Example8 Sub Main() Dim values As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Example5.PrintArray(values) ' DoubleIntegerValues(values, 5) Example5.PrintArray(values) ' ResizeIntegerArray(values, 20) Example5.PrintArray(values) ' Console.ReadLine() End Sub Sub DoubleIntegerValues(ByVal values As Integer(), ByVal count As Integer) For index As Integer = 0 To count - 1 values(index) *= 2 Next End Sub Sub ResizeIntegerArray(ByRef values As Integer(), ByVal capacity As Integer) ReDim values(capacity - 1) End Sub End Module Module Example9 Sub Main() Dim values As Integer() Example5.PrintArray(values) ' values = GetIntegerArray(10) Example5.PrintArray(values) ' Console.ReadLine() End Sub Function GetIntegerArray(ByVal capacity As Integer) As Integer() Dim integerArray(capacity - 1) As Integer GetIntegerArray = integerArray End Function End Module Module Example10 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 Example11 Sub Main() Dim values As Integer() = {1, 2, 3, 4, 5} Console.WriteLine(Example10.GetSum(values)) ' values = Nothing Console.WriteLine(Example10.GetSum(values)) ' (1) ' Console.ReadLine() End Sub End Module Module Exercise2Task Sub Main() Console.WriteLine(Exercise2Solution.GetConcatenation("abc", "def", _ "ghi")) Console.WriteLine(Exercise2Solution.GetConcatenation("abc", "def")) Console.WriteLine(Exercise2Solution.GetConcatenation("abc")) Console.WriteLine(Exercise2Solution.GetConcatenation()) ' Dim words As String() = {"abc", "def", "ghi"} Console.WriteLine(Exercise2Solution.GetConcatenation(words)) ' words = Nothing Console.WriteLine(Exercise2Solution.GetConcatenation(words)) ' Console.ReadLine() End Sub End Module Module Exercise2Solution 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 Exercise3Task Sub Main() Dim values As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Example5.PrintArray(values) ' Dim count As Integer = 5 Dim valuesCopy As Integer() valuesCopy = Exercise3Solution.GetIntegerArrayCopy(values, count) Example5.PrintArray(valuesCopy) ' Dim factor As Integer = 4 count = 3 Exercise3Solution.MultiplyIntegerValues(valuesCopy, count, factor) ' Example5.PrintArray(values) Example5.PrintArray(valuesCopy) ' count = 6 Exercise3Solution.MultiplyIntegerValues(values, count) ' Example5.PrintArray(values) Example5.PrintArray(valuesCopy) ' Console.ReadLine() End Sub End Module Module Exercise3Solution 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.