Results 1 to 4 of 4

Thread: Passing a 2D array

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 1999
    Location
    Baexem, Limburg, The Netherlands
    Posts
    5
    Hello,

    I got a slight problem...

    I would like to pass a two-dimensional array from a sub to another sub.

    Something like this:

    Dim matrix(1 to 10, 1 to 10)

    Sub CommandButton1_click()
    ...
    Call solve(matrix())
    ...
    End sub


    Sub solve(Paramarray matrix() as Variant)
    ...
    ...
    End sub


    It doesn't work...?????

    Does anyone have a solution?

    Kind regards,

    Gertjan

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Param array is expection the list of parameters, so the correct way to get it is like this:

    Code:
    Private Function MyFunction(ParamArray arrValues()) As Boolean
        'Do something here
    End Function
    The way you would have to pass it is like this:
    Call MyFunction("a","b","c")

    Assuming that I'm passing 3 string values.

    In your situation you could use array as a parameter:
    Code:
    Private Function MyFunction(arrValues() As Variant) As Bollean
        'Do something here
    End Function
    Then you can pass the array like you had:
    Code:
    Dim arrMyArray(10)
    
    Call MyFunction(arrMyArray)
    Regards,

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    ParamArray is used to pass a variable number of values. Do this instead
    Code:
    Option Explicit
    
    Sub CommandButton1_click()
    Dim matrix(1 To 10, 1 To 10) As Integer
    matrix(1, 1) = 32
    matrix(4, 3) = 99
    Call solve(matrix())
    '...
    End Sub
    
    Sub solve(matrix() As Integer)
    Dim x
    Dim y
    
    For x = 1 To 10
        For y = 1 To 10
            If matrix(x, y) <> 0 Then
                Debug.Print matrix(x, y)
            End If
        Next y
    Next x
    
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I know there are many ways, but what about storing arrays in a variant:

    Function blabla()
    dim array(x,y)
    blabla=array
    end function

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