gpt4 book ai didi

arrays - 无法使用数组成员变量分配数组

转载 作者:行者123 更新时间:2023-12-04 20:47:43 24 4
gpt4 key购买 nike

我有一个类,其中一个成员变量是一个数组。我正在尝试将数组分配给对象,但不断收到“无法分配数组”编译错误。我也很好奇如何在对象中获取数组的 UBound。 UBound(obj.array) 无法编译。我正在使用excel 07 vba。

'Test routine that keeps failing

Sub test()

Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"

Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4
MsgBox UBound(obj.ary)

End Sub


'Class declaration
Private pary() As String
Private pfield As Long

Public Property Get ary(ByVal index As Long) As String
Set ary = pary(index)
End Property

Public Property Let avry(Value() As String)
ReDim pary(UBound(Value)) As String
For i = LBound(Value) To UBound(Value)
pary(i) = Value(i)
Next i
End Property

Public Property Get field() As Long
field = pfield
End Property

Public Property Let field(Value As Long)
pfield = Value
End Property

Private Sub Class_Initialize()
pfield = 0
End Sub

最佳答案

这对我有用

Sub test()

Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"

Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4
MsgBox obj.ary(2)

End Sub

Public Property Get ary(ByVal index As Long) As String
ary = pary(index)
End Property

Public Property Let avry(vValue As Variant)
ReDim pary(UBound(vValue)) As String
Dim i As Long
For i = LBound(vValue) To UBound(vValue)
pary(i) = vValue(i)
Next i
End Property

Public Property Get field() As Long
field = pfield
End Property

Public Property Let field(Value As Long)
pfield = Value
End Property

Private Sub Class_Initialize()
pfield = 0
End Sub

正如蒂姆所说,您可以将数组作为变体传递。您的 MsgBox 正在尝试查找 String 数据类型的 UBound,所以这是一个问题。此外,您没有将参数传递给 MsgBox 中的 ary。 ary 属性返回一个字符串,但您使用的是 Set 关键字,这是另一个问题。

关于arrays - 无法使用数组成员变量分配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286767/

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