Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays as parameters?

Status
Not open for further replies.

Kimed

Programmer
May 25, 2005
104
LT
Hi,

I have a function that must fill an array that is passed to it with values. Online help states that

The arglist argument has the following syntax and parts:
[Optional] [ByVal | ByRef] [ParamArray] varname[[!]( )[/!]] [As type] [= defaultvalue]
, from which I concluded that using arrays as parameters is possible. So I have the following code:

Dim ChildRow(30) As Integer
Call ChildArray(RowForm, ChildRow())
...
Public Sub ChildArray(ByRef RowForm As String, ByRef ChildRow() As Integer)

The procedure itself seems to compile normally, but the line that calls it throws a compilation error: "Type mismatch: array or user-defined type expected"... which also suggests the possibility of array parameters, only that I'm not using it right.

Thanks.
 
you dont need the ()

Code:
Sub Bob()
Dim ChildRow(30) As Integer
Dim rowform As String

rowform = "Hello"

For i = 1 To 30
    ChildRow(i) = i
Next

Call ChildArray(rowform, ChildRow)

End Sub


Public Sub ChildArray(ByRef rowform As String, ByRef ChildRow() As Integer)
    For i = 1 To 30
        Debug.Print ChildRow(i) & " rowform"
    Next
    
End Sub


Chance,

F, G + 1MSTG
 
I tried that too - it throws the same error.
 
Weird - I tried your example and it *does* work, but mine still doesn't (without (), yes ). For the life of me, I can't see the difference between declarations.
 
An empty array is passed, then the procedure parses RowForm, separates numeric symbols from alphabetic and writes scanned numbers into the array. But I tried to populate the array before calling the procedure too, and it doesn't matter - the error is thrown when the code is compiled, not when the operator is called.
 
why are you passing an empty array ? and not a function to return the array ?

Chance,

F, G + 1MSTG
 
Huh? *Return* the array?

ChildRow=ChildArray(RowForm)
...
Public Function ChildArray(ByRef RowForm As String) As Integer()

Like this? Then it does "Can't assign to array", also at compile time.
 
Which version of which application ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Excel from MS Office 2003,... and if it's of any importance, VB's "About" shows "6.3, Version 9969 VBA:Retail 6.4.8869
 
Code:
Sub Bob()
Dim ChildRow() As Integer
Dim RowForm As String

RowForm = "Hello"
 ChildRow = ChildArray(RowForm)

For i = 1 To 30
    Debug.Print ChildRow(i)
Next

End Sub


Public Function ChildArray(ByRef RowForm As String) As Integer()
Dim child(30) As Integer

    For i = 1 To 30
       child(i) = i
    Next
    
    ChildArray = child
    
End Function

Chance,

F, G + 1MSTG
 
This works in my Excel2003:
Code:
Sub Bob()
Dim ChildRow
Dim RowForm As String
RowForm = "Hello"
ChildRow = BuildArray(RowForm)
Call ChildArray(RowForm, ChildRow)
End Sub

Public Sub ChildArray(ByRef RowForm As String, ByRef ChildRow)
Dim i As Integer
For i = 0 To UBound(ChildRow)
  Debug.Print ChildRow(i) & ", " & RowForm
Next
End Sub

Public Function BuildArray(ByRef RowForm As String) As Integer()
Dim i As Integer, myArray(30) As Integer
For i = 0 To UBound(myArray)
  myArray(i) = i
Next
BuildArray = myArray
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top