|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Arrays can be passed to methods.
Important here is to remember that an arrayvariable does not contain the array itself, but contains a reference to the array-instance.
In next example parameter values will receive a copy ( ByVal ) of the reference of the array-instances ( or Nothing (1) ) contained in the arrayvariables numbers, words and dates. So values actually points to the same array-instance as numbers, words and dates do. |
| Module Example1
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)
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 Download Broncode |
| Output : 1 2 3 4 5
Hello World
No array.
No array. |
| Because numbers, words and dates are all of a different arraytype ( Integer(), String() and Date() ) parameter values must be of a type that can hold a reference to all these arraytypes. The most suitable datatype for values is therefore Array. |
Object Datatype
| The elements of the received array-instance can be of any type, in the above example of types Integer and String. Therefore the elementvariable element had to be of type Object, which can hold any type of value.
This is also illustrated in the next example. |
| Module Example2
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 Download Broncode |
| Output : 5
15
Hello
Hello World !
True
False |
| Avoid using the Object datatype whenever possible. Working with the Object type can introduce boxing- or unboxingoperation and calls on late bound objects, which can lead to a performance overhead or even runtime type-errors.
Read the topic about value versus reference types and early versus late bound objects for more details. |
Up
Comparing References - Is and IsNot Operators
| Two references ( for instance the references holded by two arrayvariables ) can be checked for equality with the Is and IsNot operators. This in contrary to checking for equality between more primitive types where the = and <> operators are used. |
| Module Example3
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 Download Broncode |
| Output : True
True
True
True
True
True |
| 'Nothing' is a legal value to assign to a 'Array'-variable and a 'For Each'-iteration needs a reference to a collection, that's why a check like 'values IsNot Nothing' in 'Example1' was usefull. |
Up
Array as Parameter
| Parameters can work by reference ( ByRef ) with the provided arrayreferences. |
| Module Example4
Sub Main()
Dim values As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Example1.PrintArray(values)
DoubleIntegerValues(values, 5)
Example1.PrintArray(values)
ResizeIntegerArray(values, 20)
Example1.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 Download Broncode |
| Output : 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| values of DoubleIntegerValues works with a copy ( "by value" / ByVal ) of the provided arrayreference. Therefore variables values of Main and values of DoubleIntegerValues will hold the same reference and will point to the same arrayinstance.
Procedure ResizeIntegerArray wants to resize the array provided to that routine. A ReDim statement can be used for that purpose. ReDim will create a new arrayinstance, and store the reference to that new instance in the variable being used in the statement. So the content of the arrayvariable provided when calling ResizeIntegerArray needs to change, therefore we will need to work "by reference" ( ByRef ) with the provided argumentvalues. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|