|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| The position of an argumentvalue in a call to a method will determine to which argumentvariable that value is assigned.
In the following example the first argumentvalue "Hello" will be assigned to the first argumentvariable argument1. The second argumentvalue "World" will be assigned to the second argumentvariable argument2.
When optional arguments are used, commas are used to indicate the positions of the argumentvalues. The call on line (1) provides 30 as an argumentvalue for argument3, the defaultvalues 1 and 2 will be used for the first and second argumentvariables. |
| Class Example1
Public Shared Sub Main()
Test1("Hello", "World")
Test2()
Test2(10)
Test2(, 20)
Test2(, , 30)
Test2(10, 20)
Test2(10, , 30)
Test2(, 20, 30)
Test2(10, 20, 30)
Console.ReadLine()
End Sub
Public Shared Sub Test1(ByVal argument1 As String, _
ByVal argument2 As String)
Console.WriteLine(argument1 & " " & argument2)
End Sub
Public Shared Sub Test2(Optional ByVal argument1 As Integer = 1, _
Optional ByVal argument2 As Integer = 2, _
Optional ByVal argument3 As Integer = 3)
Console.WriteLine(argument1 + argument2 + argument3)
End Sub
End Class Download Broncode |
| Output : Hello World
6
15
24
33
33
42
51
60 |
Argument Passing By Name
| In Visual Basic 9.0 ( 2008 ) it is possible to use the identifiers of the argumentvariables to indicate which argumentvalues are provided. |
| Class Example2
Public Shared Sub Main()
Example1.Test1(argument1:="Hello", argument2:="World")
Example1.Test1(argument2:="World", argument1:="Hello")
Example1.Test1("Hello", argument2:="World")
Example1.Test2(argument2:=20)
Example1.Test2(10, argument3:=30)
Example1.Test2(argument1:=10, argument3:=30)
Example1.Test2(argument3:=30, argument1:=10)
Console.ReadLine()
End Sub
End Class Download Broncode |
| Output : Hello World
Hello World
Hello World
24
42
42
42 |
| The calls made on lines (1) and (2) are identical.
You can mix argument passing by position and name (3). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|