VB.Net Tutorial/Collections/Array Parameters

Материал из VB Эксперт
Версия от 12:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Pass array

Imports System
Imports Microsoft.VisualBasic
Module Test
  Dim i As Integer
  Public Sub AddOne(ByVal InputArray() As Integer)
    For i = 0 To 3
      InputArray(i) = InputArray(i) + 1
    Next
  End Sub
  Public Sub AddTwo(ByVal InputArray() As Integer)
    Dim tempArray() As Integer
    tempArray = InputArray.Clone
    For i = 0 To 3
      tempArray(i) = tempArray(i) + 2
    Next
  End Sub
  
    Sub Main()
        Dim intArray() As Integer = {6, 8, 7, 6}
        For i = 0 To 3
            Console.WriteLine("{0}", intArray(i))
        Next
        AddOne(intArray)
        For i = 0 To 3
            Console.WriteLine("{0}", intArray(i))
        Next
        AddTwo(intArray)
        For i = 0 To 3
            Console.WriteLine("{0}", intArray(i))
        Next
    End Sub
End Module
6
8
7
6
7
9
8
7
7
9
8
7

Passing arrays and individual array elements to procedures

Module Tester
   Sub Main()
      Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5}
      Dim i As Integer
      " display original elements of array1
      For i = 0 To array1.GetUpperBound(0)
         Console.WriteLine(array1(i))
      Next
      ModifyArray(array1) " array is passed by reference
      Console.WriteLine("The values of the modified array are:")
      " display modified elements of array1
      For i = 0 To array1.GetUpperBound(0)
         Console.WriteLine(array1(i))
      Next
      " array element passed by value
      ModifyElementByVal(array1(3))
      Console.WriteLine("array1(3) after " & _
         "ModifyElementByVal: " & array1(3))
      " array element passed by reference
      ModifyElementByRef(array1(3))
      Console.WriteLine("array1(3) after " & _
         "ModifyElementByRef: " & array1(3))

   End Sub " Main
   " procedure modifies array it receives (note ByVal)
   Sub ModifyArray(ByVal arrayParameter As Integer())
      Dim j As Integer
      For j = 0 To arrayParameter.GetUpperBound(0)
         arrayParameter(j) *= 2
      Next
   End Sub " ModifyArray
   " procedure modifies integer passed to it
   " original is not be modified (note ByVal)
   Sub ModifyElementByVal(ByVal element As Integer)
      Console.WriteLine("Value received in " & _
         "ModifyElementByVal: " & element)
      element *= 2
      Console.WriteLine("Value calculated in " & _
         "ModifyElementByVal: " & element)
   End Sub " ModifyElementByVal
   " procedure modifies integer passed to it
   " original is be modified (note ByRef)
   Sub ModifyElementByRef(ByRef element As Integer)
      Console.WriteLine("Value received in " & _
         "ModifyElementByRef: " & element )
      element *= 2
      Console.WriteLine("Value calculated in " & _
         "ModifyElementByRef: " & element)
   End Sub 
End Module
1
2
3
4
5
The values of the modified array are:
2
4
6
8
10
Value received in ModifyElementByVal: 8
Value calculated in ModifyElementByVal: 16
array1(3) after ModifyElementByVal: 8
Value received in ModifyElementByRef: 8
Value calculated in ModifyElementByRef: 16
array1(3) after ModifyElementByRef: 16

Testing the effects of passing array references using ByVal and ByRef

Module Tester
   Sub Main()
      Dim i As Integer
      Dim firstArray As Integer()
      Dim firstArrayCopy As Integer()
      firstArray = New Integer() {1, 2, 3}
      firstArrayCopy = firstArray
      For i = 0 To firstArray.GetUpperBound(0)
         Console.Write(firstArray(i) & " ")
      Next
      FirstDouble(firstArray)
      For i = 0 To firstArray.GetUpperBound(0)
         Console.Write(firstArray(i) & " ")
      Next
      If firstArray Is firstArrayCopy Then
         Console.WriteLine("The references are equal.")
      Else
         Console.WriteLine("The references are not equal.")
      End If
      Dim secondArray As Integer()
      Dim secondArrayCopy As Integer()
      secondArray = New Integer() {1, 2, 3}
      secondArrayCopy = secondArray
      For i = 0 To secondArray.GetUpperBound(0)
         Console.Write(secondArray(i) & " ")
      Next
      SecondDouble(secondArray)
      For i = 0 To secondArray.GetUpperBound(0)
         Console.Write(secondArray(i) & " ")
      Next
      If secondArray Is secondArrayCopy Then
         Console.WriteLine("The references are equal.")
      Else
         Console.WriteLine("The references are not equal.")
      End If
   End Sub " Main
   Sub FirstDouble(ByVal array As Integer())
      Dim i As Integer
      For i = 0 To array.GetUpperBound(0)
         array(i) *= 2
      Next
      array = New Integer() {11, 12, 13}
   End Sub 
   Sub SecondDouble(ByRef array As Integer())
      Dim i As Integer
      For i = 0 To array.GetUpperBound(0)
         array(i) *= 2
      Next
      array = New Integer() {11, 12, 13}
   End Sub 
End Module
1 2 3 2 4 6 The references are equal.
1 2 3 11 12 13 The references are not equal.