嗨所以我只是在学习 VB 对 OOP 的看法。测试时如何 Let
, Get
方法工作我创建了这个虚拟类Class1
,显然它不能被编译,因为“检测到歧义的名称:~”这个错误 VBE 突出显示类的第 2 行(一个声明 test_property 为整数)。
我不明白什么是模棱两可的?
仅供引用,我尝试用 Dim
声明该属性& Public
这些方法都没有改变任何东西。
见类(class)Class1
波纹管:
Option Explicit
Private testing_property As Integer
Public Property Let testing_property(new_value As Integer)
MsgBox "Let Box"
Let testing = new_value
End Property
Public Property Get testing_property(new_value As Integer) As Integer
MsgBox "Get Box"
End Property
我使用以下测试子调用它:
Sub Test()
Dim test_Class As Class1
Set test_Class = New Class1
With test_Class
.testing_property = "1"
Debug.Print .testing_property
End With
End Sub
最佳答案
您的 Private 属性变量和您的 let 和 get 公共(public)过程属性的重复声明。你应该命名你的变量
Private itesting_property As Integer
你也有你的让在你的得到之前。您应该在编写之前分配一个值。此外,您的 Get() 不应接受变量并将其调暗为整数,而您的 Let() 应将变量视为整数且不应调暗。
Public Property Get testing_property() As Integer
MsgBox "Get Box"
testing_property = itesting_property
End Property
Public Property Let testing_property(new_value As Integer)
MsgBox "Let Box"
itesting_property = new_value
End Property
关于vba - VBE 看到模棱两可的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129569/