In VB6 what is the difference between Property Set and Property Let?

57k views Asked by At

I have just created several Property Set methods, and they didn't compile. When I changed them to Property Let, everything was fine.

I have since studied the documentation to find the difference between Property Set and Property 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?

3

There are 3 answers

6
mwolfe02 On BEST ANSWER

Property Set is for objects (e.g., class instances)

Property Let is for "normal" datatypes (e.g., string, boolean, long, etc.)

3
Foo Bah On

Property Set is for object-like variables (ByRef) whereas Property Let is for value-like variables (ByVal)

4
wqw On

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.