How to Pass an Array in Visual Basic

  • Print this article

Arrays hold multiple values. They are variables that can be used to iterate through a list of values. When programming in Visual Basic, using arrays is a common process. The programmer can pass arrays to functions and subroutines where the code prints or manipulates the values. Passing arrays to a subroutine or function only takes a few extra lines of code.

Instructions

    • 1

      Define the array variable. An array is defined like any other variable except it uses parenthesis. The following is how to define an array that holds 3 values.
      ReDim myArrayvariable(3) As String

    • 2

      Assign variables for each index of the array. In programming, an array index starts with the number 0. The following code assigns values for the array.
      myArrayvariable(0) = "1"
      myArrayvariable(1) = "2"
      myArrayvariable(2) = "3"

    • 3

      Create the subroutine that accepts the passed variable. The parameter inside the "myRoutine" subroutine indicates that it requires an array variable. This subroutine iterates through each element of the array and prints it to the debug window.
      Sub myRoutine (myInArray( ) As String)
      Dim j as Integer
      For j=1 to 3
      Debug.Print myInArray( j )
      Next j
      End Sub

    • 4

      Pass the array to the subroutine created in step 3. This statement calls the subroutine and passes the array variable created in step two.
      myRoutine myArrayvariable()

Related Searches

References

Comments

You May Also Like

Related Ads

Featured
View Mobile Site