按项目编号访问类成员的方法

时间:2018-05-21 17:37:24

标签: vba excel-vba excel

抱歉新手问题。我是VBA班的新手。

我创建了一个cRanges类,它基本上是另一个自定义对象的集合。我为它定义了一个Item方法。但我无法理解为什么cRanges类的Item方法不起作用?

这是我的代码:

' CLASS MODULE - cRange
' Member variables
Private m_Low As String
Private m_High As String

' Properties
Property Get low() As String
    low = m_Low
End Property

Property Get high() As String
    high = m_High
End Property

Property Let low(s As String)
    m_Low = s
End Property

Property Let high(s As String)
    m_High = s
End Property

------------------

' CLASS MODULE - cRanges
' Member variables
Private m_Ranges As New Collection
Private r As New cRange

' Methods
Public Sub Add(r As cRange)
    m_Ranges.Add r
End Sub

Public Sub Remove(r As cRange)
    m_Ranges.Remove r
End Sub

Public Function Count() As Long
    Count = m_Ranges.Count
End Function

Public Function Item(i As Integer) As cRange
    If i > 0 And i <= m_Ranges.Count Then Set Items = m_Ranges.Item(i)
End Function

--------------------

Sub main()

Dim r As New cRange
Dim rr As New cRanges

r.low = "2"
r.high = "9"

rr.Add r

Debug.Print r.low
Debug.Print r.high

Debug.Print rr.Count
Debug.Print rr.Item(1).high '<-- Object variable or with block variable not set

End Sub

谢谢! .................................................. .................................

0 个答案:

没有答案