My understanding of using the Let property in a class module so far is that you set it up in the class modules like this:
Dim pName as String
Public Property Let Name(Value As String)
pName = Value
End Property
And then you after you've created an object of this class you can set this property like so:
MyObject.Name = "Larry"
Question: Is it possible to somehow enter multiple arguments into a class property? For instance:
Dim pFirstName as String, pLastName as String
Public Property Let Name(FirstName As String, LastName As String)
pFirstName = FirstName
pLastName = LastName
End Property
How would you then go about setting this property outside the class?
MyObject.Name = ??
Or is this just plain not possible to do?
As per your comment if you would prefer to encapsulate this logic then you can use something similar to the below.
Below includes the sub and function. The function returns a Person object:
Person Class:
I realise there already are 2 workarounds but I thought answering your original question was worth giving a shot due to the amount of views this question is receiving.
The answer to
is
YES! It's possible.
First let's talk about the GET property. Consider this being the
Class1
The
Name
property will return the full name, that's great but how to use theLet
property to assignfirstName
&lastName
in one go?Side note: You could pass a single string separated with a special character and split it inside the body of the
Let
and assign thefirst
andlast
names but forget that, let's get it done properly...OK, in VBA the default
Let
property for the current setup would take 1 parameter and assign it to either first or last name...Something like:
Rule: the
Get
takes no parameters and theLet
takes one. That is very logical because theGet
returns the underlying value but theLet
needs to grab a value from somewhere in order to assign it to the data it's representing. Worth remembering at this point that theLet
property is assigned via the=
sign ie.myObject.Name = "IdOnKnuu"
we know that if we stay consistent with the above rule, theoretically we should be able to add one parameter to the
Get
and one more to theLet
.Let's forget the
Let
- comment it out for now - and add a parameter to theGet
property.Going back to the
Module1
create an instance ofClass1
and typec.Name(
oh, intelli-sense expecting something? Awesome!
At this point it's worth understanding that our
Get
property returnsfirst + last
which are both empty at the moment, so it doesn't matter what you are going to pass to thec.Name()
it will return an empty string.Ok, let's uncomment and tweak the
Let
property now...Side node: if you jump back to
Module1
and typec.
and don't get intelli-sense it pretty much means that something inClass1
is broken... We already know - it's theLet
property that's causing itWe have added a parameter to the
Get
property, let's do the same with theLet
property...Let's go back to the
Module1
and try to use theLet
property:Remember, how you have used the
Let
property before? You needed to assign it a valueBut now, your
Let
property is expecting an extra parameterfirst as String
.Why don't we try this:
Hey! that compiles and runs (a quick F5)
Great, let's check the results!
Uhm... that only shows
Smith
in the Immediate Window (Ctrl+G)... Not exactly what we were looking for....Going back to the
Let
property we notice that we are grabbing 2 values via arguments but we only make use of one of them? We have the 2 different values coming in to the function, right? Let's treat the first one as thefirstName
and the second one aslastName
Going back to
Module1
and re-running out current code gives us what we need ... it prints John Smith but wait!!! why do we have to pass the first name in order to retrieve the full name?
Ha! The trick to this is to make the first parameters of both properties
Optional
Summarising, the code:
Class1.cls
Module1.bas
So basically the assignment of two (or more) values through the
Let
property is possible in VBA. The thing that may throw you off a bit is the Syntax for it but I hope my explanation in this answer has helped you understand where things come from and why.The
Get
property takes an optional parameter - it's really just a dummy parameter... it's not used anywhere within theGet
property but it's allowing us to get the desiredLet
signature and allows us to pass two parameters to it. It also grants the easy to rememberc.Name
syntax.The call
is still possible, however the
"first"
parameter just acts like a dummy and has no effect on the actual underlying data. It's a dummy and has no effect on the actual data - dump it and use:definitely more convenient :)
Use as Public Sub procedure within the class to perform this:
Person Class: