Property Let Syntax

nummyofthedowd

New Member
Joined
Apr 10, 2013
Messages
13
Hi,

I have been writing some VBA (Excel) classes and using the Property Let and Get functions. I understand the Get syntax, but not the Let. I have pasted a typical example below, could someone clarify why it does not use me.PenColor("Red") as seen below? As far as I can tell the Let property function accepts an argument but in all examples I can find (MrExcel, Chip Pearson etc) they all have a simple assignment. Why does the Let property function not require an argument when called?

Thanks in advance.
David

Code:
' Begin Class Definition
Dim CurrentColor As Integer
Const BLACK = 0, RED = 1, GREEN = 2, BLUE = 3


' Set the pen color property for a Drawing package.
' The module-level variable CurrentColor is set to
' a numeric value that identifies the color used for drawing.


Property Let PenColor(ColorName As String)
Select Case ColorName ' Check color name string.
Case "Red"
CurrentColor = RED ' Assign value for Red.
Case "Green"
CurrentColor = GREEN ' Assign value for Green.
Case "Blue"
CurrentColor = BLUE ' Assign value for Blue.
Case Else
CurrentColor = BLACK ' Assign default value.
End Select
End Property


' Returns the current color of the pen as a string.


Property Get PenColor() As String
Select Case CurrentColor
Case RED
PenColor = "Red"
Case GREEN
PenColor = "Green"
Case BLUE
PenColor = "Blue"
End Select
End Property


Sub pen()
' The following code sets the PenColor property for a drawing package
' by calling the Property let procedure.
PenColor = "Red"
'************** Why Not ********************************** 
me.PenColor("Red")
'************** Why Not **********************************


' The following code gets the color of the pen
' calling the Property Get procedure.
ColorName = PenColor


MsgBox ColorName
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,
This is syntax that would usually apply to a member of a collection (arrays, dictionaries, collections):
Code:
me.PenColor("Red")
(it is also how you might call a function, without the Me. bit)

The property you are creating is not an object of that type, so it uses an assignment syntax:
Code:
'Correct
Me.PenColor = "Red"

I may not be reading your question correctly.
Another way of answering it would be to say that the Property Let procedure exists to do exactly what you are getting confused about: to create an assignment procedure, which uses an assignment operator (=) when implemented.
 
Last edited:
Upvote 0
Thanks xenou, I think I have my head around this now and have created some more complex get and let functions successfully.

Thanks for the help!
Dave
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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