VBA:类模块:获取和释放

VBA:类模块:获取和释放,vba,class,Vba,Class,我没有自定义类的经验,还有一个非常简单的问题,但我发现用谷歌搜索很困难: 我遇到了一个使用自定义类的示例() 模块1 Sub clsRectAreaRun() 'This procedure instantiates an instance of a class, sets and calls class properties. Dim a As Double Dim b As Double Dim areaRect As New clsRectArea a = InputBox(&quo

我没有自定义类的经验,还有一个非常简单的问题,但我发现用谷歌搜索很困难:

我遇到了一个使用自定义类的示例()

模块1

Sub clsRectAreaRun()
'This procedure instantiates an instance of a class, sets and calls class properties.

Dim a As Double
Dim b As Double

Dim areaRect As New clsRectArea

a = InputBox("Enter Length of rectangle")
b = InputBox("Enter Width of rectangle")

areaRect.Length = a
areaRect.Width = b

MsgBox areaRect.rArea

End Sub
类别模块“CLSRectrea”

'Example - Create Read-Only Class Property with only the PropertyGet_EndProperty block.
Private rectL As Double
Private rectW As Double

Public Property Let Length(l As Double)
     rectL = l
End Property

Public Property Get Length() As Double
    Length = rectL
End Property

Public Property Let Width(w As Double)
   rectW = w
End Property

Public Property Get Width() As Double
    Width = rectW
End Property

Public Property Get rArea() As Double
'Read-Only property with only the PropertyGet_EndProperty block and no PropertyLet_EndProperty (or PropertySet_EndProperty) block.
    rArea = Length * Width
End Property
我的问题是关于守则的这一部分:

areaRect.Length = a
areaRect.Width = b

MsgBox areaRect.rArea 'rArea = Length * Width
从我所读到的内容来看,
Get
Let
属性具有相同的名称是一种观点。但是我必须问,代码如何知道它应该调用
Get
还是
Let
?在这种情况下,
Length
Width
是在等号的左边还是右边?如中所示,当您要为属性指定值时,它会自动识别它的
Let
,如果它位于右侧,如这里的
rArea
,则代码应该检索值,因此它是
Get


我知道,非常基本,但我不是100%确定,我只是想知道我是否弄乱了一些基本的东西。

你可以通过在类模块的代码中添加
MsgBox
,来说服自己调用了哪个属性方法

例如:

Public Property Let Length(l As Double)
     rectL = l
     MsgBox "Let Length called."
End Property

如果是对象,则指定值为
let
set
。检索值是
get
。这是一个很好的VBA类教程。@Warcupine谢谢!我能以某种方式结束我自己的问题,还是投票赞成?