CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2001
    Posts
    6

    Passing dynamic arrays into a subroutine

    How do you pass a dynamic array into a subroutine? I have filled an array:

    Dim arAssetIDs() as Integer
    .
    .
    ReDim arAssetIDs(1 to intAssetCount)




    and then tried to call a sub:

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs())




    which goes to:

    public Sub subAssetTransfer(intSourceBEMS as Integer, _
    intTargetBEMS as Integer, paramarray arAssets())




    and all I get is an overflow error when it tries to execute the call statement.


  2. #2

    Re: Passing dynamic arrays into a subroutine


    dim a() as integer
    redim a(1 to 10)

    call s_1(a)
    call s_2("pippo","pluto","paperino")


    public sub s_1(p())
    '...
    end sub

    public sub s_2(param array p())
    '...
    end sub





    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  3. #3
    Join Date
    Aug 2001
    Posts
    6

    Re: Passing dynamic arrays into a subroutine

    The above gets a data type mismatch error. Adding parends...

    call s_1(a())



    ...gets and overflow error.

    The intent is for the user to make one or more selections from a multiselection list box, and then pass the bound values to a subroutine via an array.

    Here is more of my code with different attempts:

    Dim arAssetIDs() as Integer
    Dim intAssetCount as Integer
    Dim intListCount as Integer
    Dim intArrayCounter as Integer
    Dim j as Integer

    intAssetCount = lstSourceAssets.ItemsSelected.Count - 1
    intListCount = lstSourceAssets.ListCount - 1
    ...
    ReDim arAssetIDs(0 to intAssetCount)
    intArrayCounter = 0

    for j = 1 to intListCount
    If lstSourceAssets.Selected(j) then
    arAssetIDs(intArrayCounter) = lstSourceAssets.ItemData(j)
    intArrayCounter = intArrayCounter + 1
    lstSourceAssets.Selected(j) = false
    End If
    next j

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs)
    ...
    public Sub subAssetTransfer(intSourceBEMS as Integer, _
    intTargetBEMS as Integer, arAssets())
    ...



    The above code does NOT work. The call statement generates a compile error at the call statement, highlighting the arAssetIDs argument, that says "Type mismatch: array or user defined type expected."

    If I change it to:

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs())



    ...I get the same.

    If I change it to:

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs())
    ...
    public Sub subAssetTransfer(intSourceBEMS as Integer, _
    intTargetBEMS as Integer, arAssets)



    ...it compiles ok but I get an overflow error when it tries to execute the call statement. And I get the same with:

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs)
    ...
    public Sub subAssetTransfer(intSourceBEMS as Integer, _
    intTargetBEMS as Integer, arAssets)



    I also get an overflow error with:

    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs())
    ...
    public Sub subAssetTransfer(intSourceBEMS as Integer, _
    intTargetBEMS as Integer, paramarray arAssets())




    So what am I doing wrong? There must be a way to pass a dynamic array into a subroutine. It has to be dynamic because the user could select any number of items.


  4. #4

    Re: Passing dynamic arrays into a subroutine

    U get "Type mismatch: array or user defined type expected."


    Dim arAssetIDs() as Integer
    '...
    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs)
    '--------
    public Sub subAssetTransfer(intSourceBEMS as Integer, intTargetBEMS as Integer, _
    arAssets() as integer) '<-- U must declare datra type!

    'or

    Dim arAssetIDs() '<-- as variant
    '...
    Call subAssetTransfer(cmbSourceOwner, cmbTargetOwner, arAssetIDs)
    '--------
    public Sub subAssetTransfer(intSourceBEMS as Integer, intTargetBEMS as Integer, _
    arAssets()) ) '<-- U don't have to declare data type.. default is variant





    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  5. #5
    Join Date
    Aug 2001
    Posts
    6

    Re: Passing dynamic arrays into a subroutine

    No good! Tried it both the ways you suggested. Each one gets an overflow error.


  6. #6

    Re: Passing dynamic arrays into a subroutine

    overflow error is a run time error...
    R U sure that the overflow is not in loop "for"?
    where is the break point on overflow? try to debug...




    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  7. #7
    Join Date
    Aug 2001
    Posts
    6

    Re: Passing dynamic arrays into a subroutine

    Yes, runtime error. I know it is the call statement because I put a stop just before it, then stepped into it. All is well until the call statement is executed. Then I get the overflow error.


  8. #8

    Re: Passing dynamic arrays into a subroutine

    what is cmbSourceOwner?



    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  9. #9
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Passing dynamic arrays into a subroutine

    Here's a sample... hope it helps.


    private Sub Command1_Click()
    Dim aInts_(10) as Integer
    Dim i as Integer
    Dim lSum as Long

    for i = 1 to 10
    aInts_(i) = i
    next
    lSum = SumArray(aInts_)
    MsgBox lSum

    End Sub

    private Function SumArray(byref Array_ as Variant)
    Dim j as Integer
    Dim lSum as Long

    for j = 1 to UBound(Array_)
    lSum = lSum + Array_(j)
    next j
    SumArray = lSum
    End Function






  10. #10
    Join Date
    Aug 2001
    Posts
    6

    Re: Passing dynamic arrays into a subroutine

    ****, you're good! I just found my problem and it has nothing to do with the array. I was attempting to pass a string value - cmbSourceOwner - with an integer argument, as you no doubt suspected.

    Thanks for the help. I will use the valuable info you gave me after I correct my own stupidity.


  11. #11
    Join Date
    Aug 2001
    Posts
    6

    Re: Passing dynamic arrays into a subroutine

    In all fairness to berta, the example code provided works just fine. My overflow error came from attempting to stuff a value greater than 32,767 into an integer variable, and had nothing to do with passing dynamic arrays. After correcting my blunder, and using the example provided by berta, my procedure now works just fine.

    Thanks again, berta!


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

Featured