5

Is there better way how to create function returning array than:

function foo
 Dim bar(1 to 2)as double
 bar(1)=1
 bar(2)=2
 foo=bar
end function

and in code:

arrayx=foo

Because when I declare Dim arrayx(1 to 2) as double it throws an error "can't assign array" When I don't declare the variable arrayx it doesn't seem to have any problems.

2
  • You forgot to specify the return type of your function. Commented May 19, 2011 at 8:47
  • Cody: If I knew how to specify it I would do so. When I tried to do so I obtained only error messages. Commented May 19, 2011 at 9:06

3 Answers 3

13

As Matt suggests, this error:

Compile error: Can't assign to array

stems from the fact that you've tried to assign the return value of Foo() to a fixed array, rather than a dynamic array. You simply need to indicate to the compiler that the variable you're declaring is an array, not the actual size of the array. It will figure out the size based on the size of the array that is returned.

Additionally, you should always specify a return value type for your functions. You do that in VB by placing an As Type clause at the end of the function declaration. In this case, you want an array of doubles, written as Double().

So, rewrite your code to look like this, incorporating both of those changes:

Function Foo() As Double()      ' note the specification of a return value
   Dim bar(1 To 2) As Double
   bar(1) = 1
   bar(2) = 2
   Foo = bar
End Function

Private Sub Command1_Click()
   Dim arrayx() As Double       ' note the declaration of a DYNAMIC array
   arrayx = Foo()
   MsgBox arrayx(1)
End Sub

This code displays a message box with the value "1", just as expected.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for giving a right code and explaining what I did wrong.
what if you want to return ALL the items in the array in the message box?
@Jason You need to loop the array (with LBound and UBound) and build a string with the message to be returned.
2

You can only assign to a dynamic array. Try:

Dim arrayx() as Double

Comments

0

If you are using a small array with only a few values, then you can also use byRef instead of byVal.

Private Function Foo(ByVal inputValue as Integer, Optional ByRef StrVal1 As String = "", Optional ByRef IntVal1 As Integer = 0) As String
  If inputValue<123 Then
  Foo = "Some string"
    StrVal1 = "Str one"
    IntVal1 = 27
  Else
    StrVal1 = "Str two"
    IntVal1 = 321
  End If
End Function

or

Private Sub Foo(ByVal inputValue as Integer, Optional ByRef StrVal1 As String = "", Optional ByRef IntVal12 As Integer = 0)
  StrVal1 = "Str one"
  IntVal1 = 27
End Function

Dim A$, B$, C$
A$ = Foo(1999, $B, $C)

Debug.Print A$
Debug.Print B$
Debug.Print C$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.