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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Mismatch on Array Returned by Function

Status
Not open for further replies.

dfrazell

IS-IT--Management
May 26, 2005
65
US
I'm getting a '800a000d' Type Mismatch error when I try to return a 2D array from a function. I'm passing in LineArray, sorting it and returning it. What am I missing?


dim LineArray (2, 19)
...

LineArray = SortArray (LineArray, 0)
...

------------------------------------------
function SortArray (InArray, SortCol)
------------------------------------------
...
<Sort Logic>
...
SortArray = InArray
End Function
-------------------------------------
 
Don't think you can return arrays. Just use it as a sub.
Code:
dim LineArray (2, 19)
...

SortArray LineArray, 0
...

------------------------------------------
sub SortArray (InArray, SortCol)
------------------------------------------
...
<Sort Logic>
...

End sub
-------------------------------------
 
Sure you can return an array. You probably don't need to, though, unless you specifically pass the array ByVal.

Without seeing the rest of the sort function code, and knowing which line the error occurs on, it's pretty difficult to tell what the problem is.

Lee
 
Like this.
[tt]
dim a 'no dimensions assigned
a = SortArray (LineArray, 0)
'etc etc...

------------------------------------------
function SortArray ([red]byVal[/red] InArray, SortCol)
------------------------------------------
...
<Sort Logic>
...
SortArray = InArray
End Function
[/tt]
Added byVal so that you have the original array preserved unchanged. Otherwise, by default it would be byRef on variant of array type, and it ends up having two copies of the sorted array (a and InArray). Hence, in latter case, a sub would be the simpler to comprehend.
 
So if I return the results to array "a" can I then set LineArray = a or would I have to loop through array and assign element by element?
 
[1] If you want simply LineArray holding the sorted array, _and_ you want to call the routine function rather than sub, then define the function without the byVal.
[tt]
SortArray LineArray,0
'or
'call SortArray(LineArray,0)

------------------------------------------
function SortArray (InArray, SortCol)
------------------------------------------
...
<Sort Logic>
...
SortArray = InArray
End Function
[/tt]

[2] If you want to keep the byVal so that you get back two copies one a (sorted) and another LineArray (original, unsorted), then the only way you reassign a to LineArray is to do entry by entry. To illustrate.
[tt]
for i=0 to ubound(a,1)
for j=0 to ubound(a,2)
LineArray(i,j)=a(i,j)
next
next
[/tt]
You'll that actually perform and it illustrates the point.
 
So would all this be simpler if I use sub instead of function? Meaning should I just use sub?

Thanks for everyone help and input!
 
For this particular case, it would be simpler; and you can take this line SortArray=InArray out, and use default (ByRef) implicitly or explicitly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top