gpt4 book ai didi

公共(public)变量和属性之间的VBA区别

转载 作者:行者123 更新时间:2023-12-04 04:50:01 24 4
gpt4 key购买 nike

和有什么区别

Public Variable As Integer


Private pVariable As Integer

Public Property Let Variable(ByVal lVariable As Integer)
pVariable = lVariable
End Property

Public Property Get Variable()
Variable = pVariable
End Property

在 VBA 类模块中?

为什么我要使用第二个版本?

最佳答案

尽管 VBA 是面向对象的,但它在许多方面仍然受到限制,但就本示例而言,仅了解 VBA 中 OOP 的基础知识就足够了。

您的代码

Private pVariable As Integer

Public Property Let Variable(ByVal lVariable As Integer)
pVariable = lVariable
End Property

Public Property Get Variable()
Variable = pVariable
End Property

错了有点没必要 .

注意:如果您想处理错误/验证传入的数据,您可以这样做,但通常如果它像设置和获取值一样简单,您就不会这样做。

如果要同时公开 Let/Set 和 Get 属性,为什么还需要私有(private)支持字段?为此,您只需要公共(public)变量本身,不需要属性。

当您只需要公开其中一个属性而不公开另一个属性时(即仅 setter 或 getter),故事会发生 360 度变化。也许通过一个例子更容易理解......

示例

让我们从一个简单的“银行”示例开始(显然您不会在现实生活中的 VBA 中这样做,但作为基础进行评估是一个很好的概念)

想象一下,您必须构建一个类来模拟银行帐户。你需要一种方法来 depositwithdraw来自账户的钱也显示 balance .

通常你 不会有一个 setter对于 balance字段,因为不应允许任何人显式 set余额。 (如果您知道允许这样做的银行,请告诉我;))。实际余额应为 私有(private) 多变的。应该有一个属性可以公开它,这就是您应该在这里考虑的全部。

考虑一个 VBA 类(一个接口(interface))

IAccountServices.cls
Sub Deposit(amount As Double)
End Sub

Sub WithDraw(amount As Double)
End Sub

和另一个代表帐户的类

帐户.cls
Implements IAccountServices

' balance should be private
' cause you should only have a getter for it
' you should only be able to set the balance inside this class
' based on the operations
Private accBalance As Double

' see Getter only - no setter
Public Property Get Balance() As Double
Balance = accBalance
End Property

Public Function Deposit(amount As Double)
accBalance = accBalance + amount
End Function

Public Function WithDraw(amount As Double)
accBalance = accBalance - amount
End Function

Private Sub IAccountServices_Deposit(amount As Double)
accBalance = accBalance + amount
End Sub

Private Sub IAccountServices_WithDraw(amount As Double)
accBalance = accBalance - amount
End Sub

注意:这显然是最简单的示例,它没有任何错误处理或检查余额是否足以提取等。这仅用于演示目的,不用于实际应用程序。

通过这种封装,我立即看到/知道
  • accBalance类以外的任何地方都无法访问私有(private)字段 .
  • 我只能检索 balance()并且没有在 Account 的实例上显式设置它类(class)。
  • 我可以deposit()withdraw()账户资金(公开方式)


  • 在您的标准模块(module1)中,即使使用智能感知,您也会列出 .Balance,这就是您的库/类用户所需要担心的。

    现在有一个标准编码模块来测试这两个类(Module1)
    Sub Main()

    Dim myAccount As Account
    Set myAccount = New Account

    Debug.Print "Starting Balance: " & myAccount.Balance

    myAccount.Deposit (2000)
    Debug.Print "Deposited: 2000"

    myAccount.WithDraw (250)
    Debug.Print "Withdrew: 250"

    Debug.Print "Ending Balance: " & myAccount.Balance

    ' can't set balance
    ' myAccount.Balance = 999999999999999999999999
    End Sub

    要了解 VBA OOP,我可以推荐:
  • How to use the Implements in Excel VBA
  • How to use comparison methods between class object modules in VBA in a similar manner as VB.NET?
  • 关于公共(public)变量和属性之间的VBA区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24510264/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com