confusion123
Active Member
- Joined
- Jul 27, 2014
- Messages
- 400
The following code is taken from here:
Excel VBA Custom Classes & Objects, Class Modules, Custom Events
Standard Module
Firstly, what should a be declared as (the article omits it).
Secondly, what I don't understand is when you step into the code from the standard module and get to and immediately after running this line:
the code jumps into the first line of the class module:
Before the Let Property is run, if you hover the mouse over the argument ar, it somehow has already calculated it to be l*w.
How is this possible?
Thank
Excel VBA Custom Classes & Objects, Class Modules, Custom Events
Code:
'clsRectangle
'Example - Create Class Properties by using Property Procedures, Property Let procedure accepting multiple arguments.
Private dblA As Double
Public Property Let Area(lngth As Double, wdth As Double, ar As Double)
'A Property Let procedure can accept multiple arguments, and in this case the last argument contains the value to be assigned _
to the property. This last argument in the argument list is the property value set by the calling procedure. The name and _
data type of each argument in a Property Let procedure and its corresponding Property Get procedure should be the same, except _
for the last argument in the Property Let procedure which is additional. All arguments before the last argument are passed _
to the Property Let (& Property Get) procedure.
'In the case of a Property Let procedure with a single argument (at least one argument is required to be defined), this _
argument contains the value to be assigned to the property and is the value set by the calling procedure. In this case the _
Property Get procedure will have no argument.
'It is not a usual practice to pass multiple arguments in property procedures and sub-procedures or Functions are used for this.
dblA = at
MsgBox "Arguments received - lngth: " & lngth & ", wdth: " & wdth & ", ar: " & at
End Property
Public Property Get Area(lngth As Double, wdth As Double) As Double
Area = dblA
End Property
Standard Module
Code:
Sub clsRectangleRun()
'This procedure instantiates an instance of a class, sets and calls class properties, passing multiple arguments to _
Property Let procedure:
Dim l As Double
Dim w As Double
Dim rect As New clsRectangle
l = InputBox("Enter Length of rectangle")
w = InputBox("Enter Width of rectangle")
'setting the property value - passes this data to the ar variable in the Area property:
rect.Area(l, w) = l * w
'accessing the Area property:
a = rect.Area(l, w)
MsgBox "Area of Rectangle with length " & l & ", width " & w & ", is " & a
End Sub
Firstly, what should a be declared as (the article omits it).
Code:
a = rect.Area(l, w)
Secondly, what I don't understand is when you step into the code from the standard module and get to and immediately after running this line:
Code:
rect.Area(l, w) = l * w
the code jumps into the first line of the class module:
Code:
Public Property Let Area(lngth As Double, wdth As Double, ar As Double) 'PUT A BREAK POINT HERE
Before the Let Property is run, if you hover the mouse over the argument ar, it somehow has already calculated it to be l*w.
How is this possible?
Thank
Last edited: