Class2的VBA Excel属性作为Class1中的对象

时间:2011-11-26 14:31:17

标签: class vba properties

我正在努力使用VBA类模块

头等舱模块是tDate

'--- Class1 tDate
Public d as Integer
Public m as Integer
Public y as Integer
'--- some get/let properties for d,m,y

第二类模块是tRate

'--- Class2 tRate
Public pDate1 as New tDate
Public dValue as Double
'--- Now i'm trying to code properties 

Public Property Get FirstDate() As tDate
    FirstDate = pDate1
End Property

Public Property Set FirstDate(vDate As tDate)
    pDate1 = vDate  ' <----  ???
End Property

在简单模块中,我有以下子测试类

Sub test()
    Dim myDate As New tDate 
    Dim r1 As New tRate

    myDate.SetDate "20/10/1996"

    r1.FirstDate = myDate ' <-----  "Compile error : Invalid use of property"
End Sub

将小的class1对象链接为更大的class2属性的最佳解决方案是什么?

谢谢!! 尼古拉斯


使用Set解决问题:

Public Property Get FirstDate() As tDate
    Set FirstDate = pDate1
End Property

Public Property Set FirstDate(vDate As tDate)
    Set pDate1 = vDate 
End Property

以及此处:

 Set r1.FirstDate = myDate

1 个答案:

答案 0 :(得分:3)

使用Set将对象分配给变量。

Set r1.FirstDate = myDate