I've been using VBA for Excel for simple Subs and Functions to automate tasks.
I am familiar with what Class Modules and the term Property in VBA are but all my code so far consists solely of simple Modules and UserForms and I want to delve deeper in the language.
I'm having trouble understanding the significance of Properties, specifically I'm trying to understand the difference between these two pieces of code:
Code#1
'Class Module: "clsCustomer1"
Option Explicit
Public fName As String
Public sName As String
Public PhoneNumber As String
Public PIN As String
'Module: "Test1"
Sub test1()
Dim customer1 As New clsCustomer1
With customer1
.fName = "John"
.sName = "Smith"
.PhoneNumber = "6512346590"
.PIN = "55648"
Debug.Print .fName, .sName, .PhoneNumber, .PIN
End With
End Sub
Code#2
'Class Module: "clsCustomer2"
Option Explicit
Public Property Get cl_fName(ByVal name As String) As String
cl_fName = name
End Property
Public Property Get cl_sName(ByVal name As String) As String
cl_sName = name
End Property
Public Property Get cl_PhoneNumber(ByVal number As String) As String
cl_PhoneNumber = number
End Property
Public Property Get cl_PIN(ByVal number As String) As String
cl_PIN = number
End Property
'Module: "Test2"
Sub test2()
Dim customer2 As New clsCustomer2
With customer2
Debug.Print _
.cl_fName("John"), _
.cl_sName("Smith"), _
.cl_PhoneNumber("6512346590"), _
.cl_PIN("55648")
End With
End Sub
In both cases debug.print gives the same output. I could even assign the same variables in the second case to match the first one. The actual code in the normal Module is pretty much the same in both cases, if anything the second one looks messier.
Why bother to use Properties when I can declare my variables inside a Class module?
Obviously my example is as simple as it gets, but I can't find a proper use case for Properties here.