1

I am trying to define an array of object from a function but I always get the Compile error Can't assign to array.

Here is my code:

Dim param1 As String
Dim param2 As String
Dim my_legs(1 To 4) As MyObject_Obj

Set my_legs = legBuilder(param1,param2)


private Function legbuilder (ByVal param1 As String,ByVal param2 As String)As MyObject_Obj

Dim my_legs(1 To 4) As MyObject_Obj

---Filling my Array---

legBuilder = my_legs

End Function

It seems that my function is, by design not returning the expected type (an Array of MyObject_Obj)

Am I doing it wrong?

3
  • 2
    Is MyObject_Obj a custom type you created? Where is its declaration? Commented May 9, 2018 at 5:56
  • It is an object coming from my References. I actually simplified quite much on purpose here. My point is to know if I can use function(with parameters) to return an array of custom object. Commented May 9, 2018 at 6:16
  • Show the type declaration or there's no way to answer your question. "coming from references" is not the right answer. Commented May 9, 2018 at 6:27

3 Answers 3

1
Public Function ReturnArray(param1,param2) as variant
Dim A(4) as variant
A(0) = "One"
A(1) = "Two"
A(2)= "Three"
A(3) = "Four"
ReturnArray = A()
End Function

Sub test()
 MsgBox ReturnArray("A", "B")(2)

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

Comments

0

Ignoring the rest, you can't do

Set my_legs = legbuilder(param1, param2)

Set is for objects. Your "custom" legbuilder is essentially returning an array ,so drop the Set.

1 Comment

A line like? As per @ThomasG's comment - where is the code governing the custom type you have created (MyObject_Obj). You are treating it as an array so you can't use the Set keyword. You declare things in the same way Dim x As type
0
private Function legbuilder (...) As MyObject_Obj()

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.