Passing arrays and individual array elements to procedures : Array Parameters « Collections « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Collections » Array Parameters 
8.3.2.Passing arrays and individual array elements to procedures
Module Tester

   Sub Main()
      Dim array1 As Integer() = New Integer() {12345}
      Dim As Integer

      ' display original elements of array1
      For i = 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 = 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 As Integer

      For j = 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
8.3.Array Parameters
8.3.1.Pass array
8.3.2.Passing arrays and individual array elements to procedures
8.3.3.Testing the effects of passing array references using ByVal and ByRef
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.