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, :
user2140173