用户定义的类 - 对象不支持此属性或方法

时间:2018-05-16 20:53:42

标签: vba excel-vba excel

我通过构建两个来学习用户定义的类: - 类cRange具有低和高属性。 - 类cRanges是cRange的集合。我为它定义了添加和删除方法。

我无法弄清楚下面的代码有什么问题:

主程序:

Sub main()

Dim r As New cRange
Dim rs As New cRanges

r.Low = "1"
r.High = "10"
rs.Add (r)

r.Low = "21"
r.High = "30"
rs.Add (r) '<-- Object doesn't support this property or method

Debug.Print (rs.Contains("5"))
Debug.Print (rs.Contains("15"))
Debug.Print (rs.Contains("25"))

End Sub

cRange类模块:

' 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

' Methods
Public Function Contains(s As String) As Boolean

If Val(s) >= Val(m_Low) And Val(s) <= Val(m_High) Then
    Contains = True
Else
    Contains = False
End If

End Function

cRanges类模块:

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


' 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 Contains(s As String) As Boolean

For Each r In m_Ranges
    If r.Contains(s) Then
        Contains = True
        Exit Function
    End If
Next

Contains = False
End Function

谢谢............................................. .................................................. .................................................. .................................................. ..................................................

1 个答案:

答案 0 :(得分:0)

尝试

rs.Add r '<--take out parentheses.