0

Suppose I have a class like this: it has a name and surname as attributes and a single function that msgboxes the data. Should I use the first variant of this function or the second?

Private name As String
Private surname As String

Function do_something_1() As String
     MsgBox("Hello, " & name & " " & surname)
     do_something_1 = name & " " & surname
End Function

Function do_something_2(name As String, surname As String) As String
     MsgBox("Hello, " & name & " " & surname)
     do_something_2 = name & " " & surname
End Function

And in case of the second function, would the name and surname arguments overload the class attributes? Say if the class attributes are John and Green, while the function is called with Jack and Black which would be msgboxed?

EDIT: I'm aware that in the first version the attributes can also be accessed through getters, but I prefer not to use it here.

Ans
  • 1,212
  • 1
  • 23
  • 51

1 Answers1

0

This is how the code looks with properties (as mentioned in the comments). The class is named clsHuman.

Option Explicit

Private m_sName As String
Private m_sSurname As String

Public Property Get Name() As String

    Name = m_sName

End Property

Public Property Get Surname() As String

    Surname = m_sSurname

End Property

Public Property Let Surname(ByVal sNewValue As String)

    m_sSurname = sNewValue

End Property

Public Property Let Name(ByVal sNewValue As String)

    m_sName = sNewValue

End Property

Function do_something_1() As String

    MsgBox ("Hello, " & Name & " " & Surname)
    do_something_1 = Name & " " & Surname

End Function

Function do_something_2(myName As String, mySurname As String) As String

    MsgBox ("Hello, " & myName & " " & mySurname)
    do_something_2 = myName & " " & mySurname

End Function

Once you have this class, you can call it from a module like this:

Option Explicit

Public Sub TestMe()

    Dim objHuman As New clsHuman

    objHuman.Name = "Vit"
    objHuman.Surname = "yata"

    Debug.Print objHuman.do_something_1
    Debug.Print objHuman.do_something_2("V", "D")

End Sub

As you see, do_something_1 shows msgbox with the Name of the object in the class, and the do_something_2 shows msgbox with the parameters.

Edit: Concerning the question: Say if the class attributes are John and Green, while the function is called with Jack and Black which would be msgboxed? The answer is Jack and Black. But the attributes will stay John and Green.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Why should I use getters and setters inside the class? I come from java and don't remember it being a requirement. If anything I'm used to take advantage of private attributes being visible inside all of the class. Anyways, which variant **is** better - with attributes/getters or with function arguments? You answer doesn't answer that. – Ans Aug 31 '17 at 15:21
  • @Ans - geters and setters are encapsulation. It is one of the OOP pillars, also available in Java - https://www.tutorialspoint.com/java/java_encapsulation.htm. In general, depending on what you want to do, you may use any of these two options. However, concerning that you come from Java to VBA, probably you are expecting function overloading. It is a bit different - https://stackoverflow.com/questions/64436/function-overloading-and-udf-in-excel-vba – Vityata Aug 31 '17 at 15:24
  • I used to think getters and setters are for the outer use only - to access private attributes of the class. Regardless, it wasn't what my question is about. And I don't to overload the function, I want to keep only one version, just not sure which is more correct and less error prone. – Ans Aug 31 '17 at 15:30