我通过构建两个来学习用户定义的类: - 类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
谢谢............................................. .................................................. .................................................. .................................................. ..................................................
答案 0 :(得分:0)
尝试
rs.Add r '<--take out parentheses.