Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 264
Brian Hooper Avatar asked Feb 18 '11 14:02

Brian Hooper


People also ask

What is the difference between property get set and let?

Property Let creates a procedure that sets the value of an object property — the property that you set with a Property Let sub may have any data type. Property Set creates a procedure that sets the value of an object property that references another object.

What is the difference between let and set in VBA?

Let statements can be used to assign one record variable to another only when both variables are of the same user-defined type. Use the LSet statement to assign record variables of different user-defined types. Use the Set statement to assign object references to variables.


3 Answers

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

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

like image 109
mwolfe02 Avatar answered Oct 07 '22 16:10

mwolfe02


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.

like image 34
wqw Avatar answered Oct 07 '22 16:10

wqw


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

like image 41
Foo Bah Avatar answered Oct 07 '22 16:10

Foo Bah