首页 > 解决方案 > 设置作为自定义对象的属性时,对象不支持此属性或方法

问题描述

我有两个自定义类。一个是另一个的财产。我正在尝试从类外部的 Access 表单中设置属性的值。

我在这里不知所措。我试图删除 set 关键字,结果是它执行了 Let 但未定义该值。如果我尝试将其设置为 Nothing,它也会失败。(我知道狗并不是动物的真正属性,这只是为了说明。)

'Animal class
Private dog As Dog    
Public Property Get oDog() As Dog
    Set oDog = dog
End Property '<<<Error happens here.

Public Property Let oDog(myDog As Dog)
    Set dog = value
End Property

'If I don't do this, I get an object variable or with block variable not set error.
Public Sub Class_Initialize()
    Set dog = New Dog
End Sub

'Dog class
'Dog properties and methods here.

'Form
Private oAnimal As Animal

Set oAnimal = New Animal
Set oAnimal.oDog = New Dog '<<< that fails

我的最终目标是将自定义类的对象属性设置为新对象,但来自类外部,来自表单。底线是,由于某种原因,当我调用 Set 时,调用的是 getter 而不是“字母”。我知道我做错了什么,我只是不知道它是什么。我敢肯定它是显而易见的。

标签: vbams-access

解决方案


在让您使用“myDog”然后将狗设置为值尝试将其更改为:

     Public Property Let oDog(Value As Dog)
         Set dog = Value
     End Property

推荐阅读