我的目标是从子例程返回数组。我一直在遵循this教程。这是我的代码:

Function FuncReturnArray() As String
  Dim Films(1 To 5) As String
  Films(1) = "Lord of the Rings"
  Films(2) = "Speed"
  Films(3) = "Star Wars"
  Films(4) = "The Godfather"
  Films(5) = "Pulp Fiction"
  FuncReturnArray = Films
End Function

Sub SubReturnArray()
  Dim Films() As String
  Films = FuncReturnArray()
  MsgBox Films(4)
End Sub


Compile error: Can't assign to array,编辑器指向以下行:Films = FuncReturnArray()我在做什么错?

最佳答案

如此接近-您真的要为此踢自己:

Function FuncReturnArray() As String()
  Dim Films(1 To 5) As String
  Films(1) = "Lord of the Rings"
  Films(2) = "Speed"
  Films(3) = "Star Wars"
  Films(4) = "The Godfather"
  Films(5) = "Pulp Fiction"
  FuncReturnArray = Films
End Function

Sub SubReturnArray()
  Dim Films() As String
  Films = FuncReturnArray()
  MsgBox Films(4)
End Sub


注意上面代码中的第一行-“ String”后面必须跟有“()”,以表示FuncReturnArray是一个数组。您可能以为“ FuncReturnArray”之后的方括号是这样做的,但实际上在这种情况下,它们是在引用没有参数传递给函数的事实。

09-26 06:14