Results 1 to 3 of 3

Thread: Generalised Array Function [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Question Generalised Array Function [RESOLVED]

    Here's a poser.
    How could you make a function to display all the elements in an array?
    It is easy for a 1 dimensional array.
    e.g.
    VB Code:
    1. Dim intTest(5) As Integer
    2. Dim i As Integer
    3.  
    4. 'Set the values for this example
    5. For i = 1 To UBound(intTest)
    6.     intTest(i) = i
    7. Next
    8.  
    9.  
    10. For i = 1 To UBound(intTest)
    11.     MsgBox intTest(i)
    12. Next
    But how could you make a function that would return all the elements of a multi-dimensional array?
    The function would have to work for any (random) number of dimensions in the array.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Does this work?
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim x(0 To 4, 0 To 4, 0 To 4, 0 To 4) As String
    3. Dim a, b, c, d
    4.  
    5.     For a = 0 To 4
    6.         For b = 0 To 4
    7.             For c = 0 To 4
    8.                 For d = 0 To 4
    9.                     x(a, b, c, d) = a & "-" & b & "-" & c & "-" & d
    10.                 Next
    11.             Next
    12.         Next
    13.     Next
    14.  
    15.     PrintArray x
    16.  
    17.     MsgBox "Done!"
    18.  
    19. End Sub
    20.  
    21. Private Sub PrintArray(Source As Variant)
    22. Dim v As Variant
    23.  
    24.     For Each v In Source
    25.         Debug.Print v
    26.     Next
    27.  
    28. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    A stroke of genius!
    I was half expecting you to be the one to solve it.
    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width