有人可以解释为什么我在第12行会收到此错误吗?这显然是一个阵列。为什么我不能以这种方式获取索引位置0的值?我真的需要那个额外的变量(arr)吗?
Option Explicit
Dim obj
Set obj = new ClsTest
obj.singleval = "test"
MsgBox obj.singleval ' test
obj.repeatingval = split ("a,b,c", ",")
MsgBox IsArray(obj.repeatingval) ' true
MsgBox UBound(obj.repeatingval) ' 2
MsgBox obj.repeatingval(0) ' Wrong number of arguments or invalid
' property assignment: 'repeatingval'
Dim arr : arr = obj.repeatingval
MsgBox IsArray(arr) ' true
MsgBox UBound(arr) ' 2
MsgBox arr(0) ' a
Class ClsTest
Private m_singleval
Private m_repeatingval
Public Property Get singleval()
singleval = m_singleval
End Property
Public Property Let singleval(w)
m_singleval = w
End Property
Public Property Get repeatingval()
repeatingval = m_repeatingval
End Property
Public Property Let repeatingval(w)
m_repeatingval = w
End Property
End Class
答案 0 :(得分:1)
如果要对(数组)属性repeatingval
进行索引访问,则需要更改属性定义以包含索引。但要注意,必须定义getter和setter:
Class ClsTest
...
Public Property Get repeatingval(i)
repeatingval = m_repeatingval(i)
End Property
Public Property Let repeatingval(i, w)
m_repeatingval(i) = w
End Property
End Class
你不能拥有一个属性,其中setter获取一个数组,getter返回该数组的元素。为了能够分配数组并检索该数组的元素,您需要2个不同的属性:
Class ClsTest
...
Public Property Get repeatingval(i)
repeatingval = m_repeatingval(i)
End Property
Public Property Let repeatingval(i, w)
m_repeatingval(i) = w
End Property
Public Property Get repeatingarr
repeatingval = m_repeatingval
End Property
Public Property Let repeatingarr(w)
m_repeatingval = w
End Property
End Class
Set obj = New ClsTest
obj.repeatingarr = Split("a,b,c", ",")
MsgBox IsArray(obj.repeatingarr)
MsgBox UBound(obj.repeatingarr)
MsgBox obj.repeatingval(0)
答案 1 :(得分:0)
Dim thing
For Each thing in obj.repeatingval
msgbox thing
Next
这将允许您访问它。