30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

266 Arrays Chapter 7<br />

48 ' declare array references<br />

49 Dim secondArray As Integer()<br />

50 Dim secondArrayCopy As Integer()<br />

51<br />

52 ' allocate secondArray and copy its reference<br />

53 secondArray = New Integer() {1, 2, 3}<br />

54 secondArrayCopy = secondArray<br />

55<br />

56 Console.WriteLine(vbCrLf & "Test passing array " & _<br />

57 "reference using ByRef.")<br />

58 Console.Write("Contents of secondArray before " & _<br />

59 "calling SecondDouble: ")<br />

60<br />

61 ' print contents of secondArray before procedure call<br />

62 For i = 0 To secondArray.GetUpperBound(0)<br />

63 Console.Write(secondArray(i) & " ")<br />

64 Next<br />

65<br />

66 ' pass secondArray using ByRef<br />

67 SecondDouble(secondArray)<br />

68<br />

69 Console.Write(vbCrLf & "Contents of secondArray " & _<br />

70 "after calling SecondDouble: ")<br />

71<br />

72 ' print contents of secondArray after procedure call<br />

73 For i = 0 To secondArray.GetUpperBound(0)<br />

74 Console.Write(secondArray(i) & " ")<br />

75 Next<br />

76<br />

77 ' test whether the reference was changed by SecondDouble<br />

78 If secondArray Is secondArrayCopy Then<br />

79 Console.WriteLine(vbCrLf & "The references are " & _<br />

80 "equal.")<br />

81 Else<br />

82 Console.WriteLine(vbCrLf & "The references are " & _<br />

83 "not equal.")<br />

84 End If<br />

85<br />

86 End Sub ' Main<br />

87<br />

88 ' procedure modifies elements of array and assigns<br />

89 ' new reference (note ByVal)<br />

90 Sub FirstDouble(ByVal array As Integer())<br />

91 Dim i As Integer<br />

92<br />

93 ' double each element value<br />

94 For i = 0 To array.GetUpperBound(0)<br />

95 array(i) *= 2<br />

96 Next<br />

97<br />

98 ' create new reference and assign it <strong>to</strong> array<br />

99 array = New Integer() {11, 12, 13}<br />

100 End Sub ' FirstDouble<br />

Fig. Fig. 7.9 7.9 Passing an array reference with ByVal and ByRef (part 2 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!