Return array from function

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Return array from function

Post by agibsonsw »

Hello.

I can return an array from a function, but have to assign it to a Variant. Is it not possible to
assign the return value (an array) directly to another array of the same dimensions?

Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Return array from function

Post by NYIntensity »

Certainly; if this is in response to the question you had in the thread I started, simply create your second array (if you've got an array with 10 items, it would look like this)

Code: Select all

For intCount=0 to 9
     Range2(intCount)=Range(intCount)
Next intCount
If that's not what you're trying to do I'm gonna need more explanation.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Return array from function

Post by agibsonsw »

Hi. I have a function:

Code: Select all

Function GetArray() As String()
Dim temp(1 To 3) As String
' fill temp()
GetArray = temp()  ' to return the array
End Function
' in my Sub, I want to use
myArray() = GetArray()
but I can only do so if myArray is declared as a Variant (not an array).
Hope this helps, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Return array from function

Post by NYIntensity »

I don't see myArray being declared at all...

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Return array from function

Post by agibsonsw »

Sorry, I omitted this:
Sub UseGetArray()
Dim myArray(1 To 3) As String
myArray() = GetArray() 'causes an error.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Return array from function

Post by rory »

You need a dynamic array:

Code: Select all

Sub UseGetArray()
Dim myArray() As String
myArray() = GetArray()
End Sub
Regards,
Rory

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Return array from function

Post by agibsonsw »

That's fab. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.