VBA difference between shared variable and property

What's the difference between

Public Variable As Integer

and

Private pVariable As Integer

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

Public Property Get Variable()
    Variable = pVariable
End Property

in a VBA class module?

And why should I use the second version?

+4
source share
2 answers

While VBA is object oriented, it is still limited in many ways, but as far as this example is concerned, it should be enough to just understand the basics of OOP in VBA.

Your code

Private pVariable As Integer

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

Public Property Get Variable()
    Variable = pVariable
End Property

wrong a little extra .

NOTE. You can do this in cases where you want to handle errors / check the data entering the system, but usually, if it is as simple as setting up and getting the value, you would not do that.

- , Let/Set Get? , - public .

360 , , (.. , ). , , ...

" " (, VBA , )

, . deposit withdraw , balance.

< < <setter balance, set . ( , , , ;)). private. , , , .

VBA ()

IAccountServices.cls

Sub Deposit(amount As Double)
End Sub

Sub WithDraw(amount As Double)
End Sub

Account.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 , .

  • balance() Account.

  • deposit() withdraw() ( )


(module1) intelli-sense .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

VOB OOP, :

+16

, , .

Get "", . . Mass Get .

Private density as Double
Private volume as Double
Private potentialEnergy

Public Property Get mass() As Double
   mass = density*volume
End Property 'Property Get mass

A , . . :

Public Property Let density(rho as Double)
   if rho > 0 then
      density = rho
      potentialEnergy = density * volume * gravity * height
End Property 'Property Get mass

read-only ( - ), Let Get.

, , , , .

+2

Source: https://habr.com/ru/post/1546680/


All Articles