如何编写我的类模块以便可以调用属性上的属性?
我不确定我是否使用了正确的术语,因此我将尽力澄清。在MsAccess中,每当我想操作表单上的元素时,我都可以使用句点来引用它们来分隔每个对象。例如,如果我想更改文本框的值,我可以调用:
form("formname").txtboxname.value = "new value"
所以我的表格对象有一个带有值对象的文本框对象。
我怎样才能在自己的班级模块中实现这一目标。
我的具体示例是我在类中的私有变量中存储了一个数组,但是我不能简单地使用Property GET来返回数组。 (而且我不想公开它,因为数组是以编程方式填充的)但是如果我想迭代,我需要知道该数组的Ubound和Lbound值。
我宁愿避免将Ubound和Lbound值存储在自己的变量中,因为这似乎是浪费。
我怎么能以某种方式编写类来获得一个?子类?
所以,如果我想要ubound或lbound我可以调用像
这样的东西set x = mycls
debug.? x.pArrayVariable.getLBound
即使是我正在尝试做的事情的正确术语也可以让我更接近答案,我已经尝试过搜索属性和子属性,但我不确定是什么把我带到某个地方。
我班级的例子:mycls
Private pArrayVariable() as string
public property get pArrayVariable() as string
'Run Code to Populate array here
Array() = pArray()
end property
有什么叫做"收藏"我在问什么?
答案 0 :(得分:1)
因此,属性可以返回具有自己属性的对象(如用户类)。示例如下:
以下是名为MinMax
Private m_min As Integer
Private m_max As Integer
Public Property Get MinValue() As Integer
MinValue = m_min
End Property
Public Property Let MinValue(ByVal x As Integer)
m_min = x
End Property
Public Property Get MaxValue() As Integer
MaxValue = m_max
End Property
Public Property Let MaxValue(ByVal x As Integer)
m_max = x
End Property
Public Sub SetMinMax(ByVal min_value As Integer, ByVal max_value As Integer)
m_min = min_value
m_max = max_value
End Sub
Private Sub Class_Initialize()
m_min = 0
m_max = 1
End Sub
以下是名为MyClass
的类的代码。请注意它如何公开类型为MinMax
Private m_target As MinMax
Private m_name As String
Public Property Get Target() As MinMax
Target = m_target
End Property
Public Property Get Name() As String
Name = m_name
End Property
Private Sub Class_Initialize()
Set m_target = New MinMax
m_name = vbNullString
End Sub
Public Sub SetValues(ByVal a_name As String, ByVal min_value As Integer, ByVal max_value As Integer)
m_name = a_name
m_target.SetMinMax min_value, max_value
End Sub
现在主代码可以有一个类似
的语句Public Sub Test()
Dim t As New MyClass
t.SetValues "Mary", 1, 100
Debug.Print t.Target.MinValue, t.Target.MaxValue
End Sub
答案 1 :(得分:0)
我仍然对上面的原始问题感到好奇,但是它出现了无法访问阵列的问题。似乎我不对。
您可以使用
Public Property Get ArrayVariable() As String()
Call 'code to populate array
ArrayVariable= pArrayVariable() 'Notice the paren here
End Property
然后引用数组
debug.? ubound(clsvar.ArrayVariable()) 'Notice paren here too
or
debug.? clsvar.ArrayVariable()(1) 'Notice the parens here too