如何使用函数返回的字符串数组?

时间:2022-05-31 23:58:23

I have a function that returns a string array

我有一个返回字符串数组的函数

Function getAr() As String
Dim tmpAr(3) As String
getAr(0) = "Hallo"
getAr(1) = "I"
getAr(2) = "Am"
getAr(3) = "I"
getAr = tmpAr
End Function

In a Sub I want to reassign the returned string array like

在子数组中,我想重新分配返回的字符串数组

Sub test()
Dim tmpAr() As String
ReDim tmpAr(UBound(getAr))
Debug.Print tmpAr(0)
Debug.Print tmpAr(1)
End Sub

The error is

错误的是

assigning to data field is not possible.

向数据字段赋值是不可能的。

I guess that is because I have to dimension the strAr to the same dimension as the myfunc arry. But I just do not get that information.

我猜那是因为我要把弦的维度和myfunc arry的维度相同。但我就是不知道。

Could anyone help me here?

有人能帮我一下吗?

2 个解决方案

#1


2  

You don't need to know the dimension to assign an array, but your code wasn't correct.

您不需要知道分配数组的维度,但是您的代码不正确。

Here is my amended version :

以下是我修改后的版本:

Function getAr() As String()

Dim tmpAr(3) As String

tmpAr(0) = "Hallo"
tmpAr(1) = "I"
tmpAr(2) = "Am"
tmpAr(3) = "I"

getAr = tmpAr
End Function

Notice the String(), this is how you declare the type of a function returning an array.

注意String(),这就是如何声明返回数组的函数的类型。

Also, you were dimensionning tmpAr and tried to affect values to getAr which wasn't set and as you started, it is much easier to work with a temp variable that you'll assign to the function's output at the end of the manipulations.

另外,您正在对tmpAr进行维数,并试图影响未设置的getAr的值,在开始时,使用临时变量要容易得多,您将在操作结束时将其分配给函数的输出。

And your ReDim ReDim tmpAr(UBound(getAr)) was working but did not pass the array, this how you do it tmpAr = getAr and you don't even need to use ReDim before! ;)

而且您的ReDim tmpAr(UBound(getAr))正在工作,但是没有传递数组,这是如何做到的tmpAr = getAr,您以前甚至不需要使用ReDim !,)

Sub test()
Dim tmpAr() As String
tmpAr = getAr

Debug.Print tmpAr(0)
Debug.Print tmpAr(1)

End Sub

#2


3  

Your function and sub both need changing:

您的函数和子函数都需要更改:

Function getAr() As String()
    Dim tmpAr(3)              As String
    tmpAr(0) = "Hallo"
    tmpAr(1) = "I"
    tmpAr(2) = "Am"
    tmpAr(3) = "I"
    getAr = tmpAr
End Function

Sub test()
    Dim tmpAr()               As String
    tmpAr = getAr
    Debug.Print tmpAr(0)
    Debug.Print tmpAr(1)
End Sub

#1


2  

You don't need to know the dimension to assign an array, but your code wasn't correct.

您不需要知道分配数组的维度,但是您的代码不正确。

Here is my amended version :

以下是我修改后的版本:

Function getAr() As String()

Dim tmpAr(3) As String

tmpAr(0) = "Hallo"
tmpAr(1) = "I"
tmpAr(2) = "Am"
tmpAr(3) = "I"

getAr = tmpAr
End Function

Notice the String(), this is how you declare the type of a function returning an array.

注意String(),这就是如何声明返回数组的函数的类型。

Also, you were dimensionning tmpAr and tried to affect values to getAr which wasn't set and as you started, it is much easier to work with a temp variable that you'll assign to the function's output at the end of the manipulations.

另外,您正在对tmpAr进行维数,并试图影响未设置的getAr的值,在开始时,使用临时变量要容易得多,您将在操作结束时将其分配给函数的输出。

And your ReDim ReDim tmpAr(UBound(getAr)) was working but did not pass the array, this how you do it tmpAr = getAr and you don't even need to use ReDim before! ;)

而且您的ReDim tmpAr(UBound(getAr))正在工作,但是没有传递数组,这是如何做到的tmpAr = getAr,您以前甚至不需要使用ReDim !,)

Sub test()
Dim tmpAr() As String
tmpAr = getAr

Debug.Print tmpAr(0)
Debug.Print tmpAr(1)

End Sub

#2


3  

Your function and sub both need changing:

您的函数和子函数都需要更改:

Function getAr() As String()
    Dim tmpAr(3)              As String
    tmpAr(0) = "Hallo"
    tmpAr(1) = "I"
    tmpAr(2) = "Am"
    tmpAr(3) = "I"
    getAr = tmpAr
End Function

Sub test()
    Dim tmpAr()               As String
    tmpAr = getAr
    Debug.Print tmpAr(0)
    Debug.Print tmpAr(1)
End Sub