In VB6 what is the difference between Property Set and Property Let?
Solution 1
Property Set
is for objects (e.g., class instances)
Property Let
is for "normal" datatypes (e.g., string, boolean, long, etc.)
Solution 2
Property Let
is more versatile than Property Set
. The latter is restricted to object references only. If you have this property in a class
Private m_oPicture As StdPicture
Property Get Picture() As StdPicture
Set Picture = m_oPicture
End Property
Property Set Picture(oValue As StdPicture)
Set m_oPicture = oValue
End Property
Property Let Picture(oValue As StdPicture)
Set m_oPicture = oValue
End Property
You can call Property Set Picture
with
Set oObj.Picture = Me.Picture
You can call Property Let Picture
with both
Let oObj.Picture = Me.Picture
oObj.Picture = Me.Picture
Implementing Property Set
is what other developers expect for properties that are object references but sometimes even Microsoft provide only Property Let
for reference properties, leading to the unusual syntax oObj.Object = MyObject
without Set
statement. In this case using Set
statement leads to compile-time or run-time error because there is no Property Set Object
implemented on oObj
class.
I tend to implement both Property Set
and Property Let
for properties of standard types -- fonts, pictures, etc -- but with different semantics. Usually on Property Let
I tend to perform "deep copy", i.e. cloning the StdFont
instead of just holding a reference to the original object.
Solution 3
Property Set
is for object-like variables (ByRef) whereas Property Let
is for value-like variables (ByVal)
Brian Hooper
I started off programming in BBC Basic and 6502 assembler. I got my first job programming in FORTRAN on VMS, and since then have picked up C, C++ and SQL on VMS and Unix, with excursions into VB and IBM 360 assembler. I drink real ale, play wargames and contract bridge, and walk in the country. And, yes, I have a beard.
Updated on October 25, 2020Comments
-
Brian Hooper over 3 years
I have just created several
Property Set
methods, and they didn't compile. When I changed them toProperty Let
, everything was fine.I have since studied the documentation to find the difference between
Property Set
andProperty Let
, but must admit to being none the wiser. Is there any difference, and if so could someone offer a pointer to a proper explanation of it? -
Bob77 about 13 yearsIt is unrelated to WithEvents, it is for returning object references as properties. There are many times when you want to do this, most often when you have any kind of object hierarchy.
-
Bob77 about 13 yearsIt isn't a ByRef vs. ByVal issue, but is strictly used for returning object references to the caller.
-
mwolfe02 about 13 yearsWrong.
Property Get
is used for returning object references as properties, notProperty Set
. -
mwolfe02 about 13 yearsmsdn.microsoft.com/en-us/library/aa266202(VS.60).aspx: Like a
Function
andProperty Get
procedure, aProperty Set
procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike aFunction
andProperty Get
procedure, both of which return a value, you can only use aProperty Set
procedure on the left side of an object reference assignment (Set
statement). -
mwolfe02 about 13 yearsIt's true that it's not a ByRef/ByVal issue. But it is not used for returning object references to the caller. For that you use
Property Get
the same as you would for returning any other class property (there is no object-specific version for returning class properties). -
MarkJ about 13 years+1 Correct that
Property Set
defines a property that can be used on the left hand side of aSet
statement. I do think the mention ofWithEvents
is a bit confusing though, it's not really related to explaining the difference.Property Set
can be useful for 1:1 relationships between objects that can be changed dynamically at runtime, i.e. beyondWithEvents
. -
Bob77 about 13 yearsYes, I had that exactly backwards, it's for assigning to an object property - duh, really bad goof there.
-
Bob77 about 13 yearsYes, for "setting" a property - I had this 180 degrees backwards, mea culpa. In the end it's as simple as reading the manual as pointed out above.
-
mwolfe02 about 13 years@Bob: +1 for admitting a mistake. I apologize if my tone came across as harsh, but I wanted to clear up the technical issue. And while we're issuing mea culpas, I'll admit my original wording unnecessarily confused the issue of
Property Set
andWithEvents
. When I was first learning about classes in VB6/VBA I had trouble understanding when you would want to use theProperty Set
statement. I simply wanted to provide the OP with a common example. To suggest it was the 'only' situation whereProperty Set
would be useful was a mistake on my part. Hopefully the new wording is clearer. -
Panzercrisis almost 7 yearsAssuming you have both
Property Set
andProperty Let
for an object type, and assuming the implementing code is the same (like in your example), does VB6 have any built-in difference in what it'll actually do when the developer calls either property? For instance, will VB6 implicitly decide to suddenly pass the object by value, instead of by reference, if the developer calls theLet
property, or are there any other little nuances like that? (I know you said the ones you like to write have the separate semantics, but I'm not sure if the language does anything different on its own.) -
wqw almost 7 yearsIn VB6 there is no "pass the object by value" option for params. Using
ByVal oValue As StdPicture
vsByRef oValue As StdPicture
changes the way the object reference is passed. It's likeStdPicture *oValue
vsStdPicture **oValue
in C/C++ -- always pointers but in the second case one has the ability to change the reference (e.g. assign ref to a new object) inside the procedure and the caller can receive this new reference. So there is no "copy" option per se and I'm usually usingClone
method (e.g. onIFont
interface) insideProperty Let
to manually impl "deep copy" semantics. -
Panzercrisis almost 7 yearsFor reference types, I mean passing the reference by value, like in VB.NET/C#.
-
Marcelo Scofano Diniz over 3 yearsI adopted @wqw's two last paragraphs as my personal coding rule since I read it and had the need of deep copy Collections and Class instances; it facilitates too much on a glance know if it's only a pointer or a clone...