vba 直接使用私有属性 vs 使用 Get 和 Let/Set

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22079003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:09:18  来源:igfitidea点击:

Using a Private Property directly vs using Get and Let/Set

classvbaooppropertiesprivate

提问by Rick supports Monica

Beginner VBA programmer here (and beginner to programming in general) looking to learn more about how effective OOP is done.

初学者 VBA 程序员(以及一般的编程初学者)希望了解有关如何有效执行 OOP 的更多信息。

Can someone explain or provide a reference discussing the benefits/purpose of using - INSIDE of a class module - Private Property Getand/or Let/Setstatements in VBA vs accessing properties directly although no manipulation is required of the data?

有人可以解释或提供参考来讨论使用 - 类模块的内部 -Private Property Get和/或Let/Set语句在 VBA 中与直接访问属性的好处/目的,尽管不需要对数据进行操作?

Example:

例子:

I have created a cDimensionclass (in Excel). This class draws lines as Shapeobjects, and a few other things that are not relevant. The DrawingScalevariable allows the entire drawing to be scaled as desired. Some of the properties require manipulation when Getting/Setting, others don't.

我创建了一个cDimension类(在 Excel 中)。这个类将线条绘制为Shape对象,以及一些其他不相关的东西。该DrawingScale变量允许根据需要缩放整个图形。有些属性在获取/设置时需要操作,有些则不需要。

So, for example, pWidthneeds to be scaled going in:

因此,例如,pWidth需要扩展:

'clsDimension
Private pWidth As Single    
Private Property Get Width() As Single
    Width = pWidth
End Property
Private Property Let Width(w As Single)
    pWidth = w / DrawingScale
End Property

But pColordoes not require any manipulation, in or out:

pColor不需要任何操作,输入或输出:

Private pColor As Integer
Private Property Get Color() As Integer
    Color = pColor
End Property
Private Property Let Color(c As Integer)
    pColor = c
End Property

The pWidth property is an instance where using the Private Property Get and Let methods for procedures inside of the class itself makes sense to me. However, my question is: is there any reason to also use Private Property methods to Get and Let/Set the pColor property as well, as I have given them above?

pWidth 属性是一个实例,其中对类本身的过程使用私有属性 Get 和 Let 方法对我来说很有意义。但是,我的问题是:是否有任何理由也使用私有属性方法来获取和让/设置 pColor 属性,就像我上面给出的那样?

Public Function Line(sht As Worksheet, L As tLine, Optional c = vbBlack) As Shape
    Width = DistanceBetweenTwoPoints(L.first.x, L.first.y, _
                                     L.second.x, L.second.y) '<-- pWidth is scaled
    Color = c '<-- Vs. just using pColor = c
    Set Line = sht.Shapes.AddLine(L.first.x, L.first.y, L.second.x, L.second.y)
End Function

Thanks in advance.

提前致谢。

采纳答案by mwolfe02

If you are not manipulating the values on the way in or way out, just use public variables. VBA is more like Python than Java or C++ in that there is no real penalty for switching from public variables to "getter/setter" functions down the road.

如果您不在进出路中操作值,只需使用公共变量。VBA 比 Java 或 C++ 更像 Python,因为从公共变量切换到“getter/setter”函数并没有真正的惩罚

Let's say you start with the following code:

假设您从以下代码开始:

'clsCar
Public Speed As Double
Public Sub Drive()
    MsgBox "Driving at " & Speed & " MPH"
End Sub    


'Module1
Sub DriveCar()
    Set Car = New clsCar
    Car.Speed = 100
    Car.Drive  'shows msg:  "Driving at 100 MPH"
End Sub

Then you decide that driving that fast is dangerous, so you want to add a "governor" to your vehicle and need to update your class:

然后你决定开这么快是危险的,所以你想给你的车辆添加一个“州长”并需要更新你的类:

'clsCar
Private mSpeed As Double
Private Const SpeedLimit As Double = 55

Public Property Get Speed()
    Speed = mSpeed
End Property
Public Property Let Speed(Val As Double)
    If Val > SpeedLimit Then
        mSpeed = SpeedLimit
    ElseIf Val < 0 Then
        mSpeed = 0
    Else
        mSpeed = Val
    End If
End Property
Public Sub Drive()
    MsgBox "Driving at " & Speed & " MPH"
End Sub    


'Module1
'Note that no changes to this code are necessary; the change
'   is entirely encapsulated within the class (as it should be)
Sub DriveCar()
    Set Car = New clsCar
    Car.Speed = 100
    Car.Drive  'shows msg:  "Driving at 55 MPH"
End Sub

Tim Williams's comment is what you will often hear as justification for unnecessarily using Get/Let/Set in VBA, but that's the byproduct of good advice from other languages (C++ and Java, notably) being misapplied to VBA/VB6.

Tim Williams 的评论是您经常听到的在 VBA 中不必要地使用 Get/Let/Set 的理由,但这是其他语言(尤其是 C++ 和 Java)的好建议被误用于 VBA/VB6 的副产品。

回答by RubberDuck

As always.... it depends. Your x property should definitely be accessed via the letter because the letter performs a calculation on the input.

一如既往......这取决于。您的 x 属性绝对应该通过字母访问,因为字母对输入执行计算。

'clsDimension
Private Let Width(w As Single)
    pWidth = w / DrawingScale
End Property

You should NOTaccess the pWidthvariable directly. If you did, you would have to duplicate the pWidth = w / DrawingScalelogic elsewhere in your module.

你应该访问pWidth直接变量。如果这样做,则必须pWidth = w / DrawingScale在模块的其他地方复制逻辑。

Your pColorproperty couldbe a public variable, because there's no logic in getting or setting it. I don't recommend it though. What if later you realize you don't want to allow certain colors? Then you would need logic behind it and you would need to switch to a property anyway. The property is easier to maintain and more semantically correct.

您的pColor属性可能是一个公共变量,因为获取或设置它没有逻辑。不过我不推荐。如果后来你意识到你不想允许某些颜色怎么办?然后你需要它背后的逻辑,无论如何你都需要切换到一个属性。该属性更易于维护且在语义上更正确。