Passing argument in class

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

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:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
When passing an argument to a macro/function/property you need to format it as such

Code:
Sub(Argument)

When passing more than one argument you need to format it like this:

Code:
Sub Argument1, Argument2

If you are calling a sub/function/property that has an argument(s) you will need to have the same amount of arguments given when called. For example, if the sub is structured with:
Code:
Sub Something(Str as String, DB as Double, Ing as Integer)

you need to call it with the same amount of arguments, like so:
Code:
Sub Str, Db, Ing

modify your code to match the above references and see if that helps.
 
Upvote 0
When stepping through the code you are taken to the Property Let routine, as is expected.
When hovering, it evaluates the expession. To read the expression it has to read the line (to parse what you are hovering on). My guess is that this causes the line to be exicuted before the value is shown.

The interpreter that evaluates values on hover is different than the VBA code evaluator.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top