Click to See Complete Forum and Search --> : Passing dynamic arrays into a subroutine


dremlap
August 30th, 2001, 04:30 PM
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.

berta
August 30th, 2001, 04:46 PM
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/images/bertaplanet.gif'>
</center>

dremlap
August 31st, 2001, 10:41 AM
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.

berta
August 31st, 2001, 10:50 AM
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/images/bertaplanet.gif'>
</center>

dremlap
August 31st, 2001, 11:04 AM
No good! Tried it both the ways you suggested. Each one gets an overflow error.

berta
August 31st, 2001, 11:07 AM
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/images/bertaplanet.gif'>
</center>

dremlap
August 31st, 2001, 11:17 AM
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.

berta
August 31st, 2001, 11:31 AM
what is cmbSourceOwner?



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

DSJ
August 31st, 2001, 11:35 AM
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

dremlap
August 31st, 2001, 11:35 AM
****, 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.

dremlap
August 31st, 2001, 12:09 PM
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!