gpt4 book ai didi

arrays - 从子例程返回数组给出 : Compile error: Can't assign to array

转载 作者:行者123 更新时间:2023-12-03 01:59:43 25 4
gpt4 key购买 nike

我的目标是从子例程返回数组。我一直在关注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

编译错误:无法分配给数组并且编辑器指向以下行: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”后面的括号执行了此操作,但实际上在该上下文中,它们是指没有参数传递给函数的事实。

关于arrays - 从子例程返回数组给出 : Compile error: Can't assign to array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com