tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
Looking at the article:
I have a couple of questions.
This is a standard module:
This is the class Account:
This is the class IAccountServices:
In class Account, shouldn't these two functions be Subs?
Also because Implements has been added to the class Account, these two Subs are required:
but I can't see where they're used, so wouldn't it be better to remove the word Implements, as well as these two Subs?
Thanks
Code:
https://stackoverflow.com/questions/24510264/vba-difference-between-public-variable-and-property
I have a couple of questions.
This is a standard module:
Code:
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
This is the class Account:
Code:
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
This is the class IAccountServices:
Code:
Private Sub IAccountServices_Deposit(amount As Double)
accBalance = accBalance + amount
End Sub
Private Sub IAccountServices_WithDraw(amount As Double)
accBalance = accBalance - amount
End Sub
In class Account, shouldn't these two functions be Subs?
Code:
Public Function Deposit(amount As Double)
accBalance = accBalance + amount
End Function
Public Function WithDraw(amount As Double)
accBalance = accBalance - amount
End Function
Also because Implements has been added to the class Account, these two Subs are required:
Code:
Private Sub IAccountServices_Deposit(amount As Double)
accBalance = accBalance + amount
End Sub
Private Sub IAccountServices_WithDraw(amount As Double)
accBalance = accBalance - amount
End Sub
but I can't see where they're used, so wouldn't it be better to remove the word Implements, as well as these two Subs?
Thanks