On Sep 29, 2006, at 11:05 PM, Robert Livingston wrote:
When is something a property and when a method?
The syntax of their use seems to be the same. What makes one a
property and the other a method? Its seems strange to me that you
can assign a value to a method. (ListBox1.Cell(1,2) = "Dog")
A true property is a value which is stored at a specific memory
address. Getting and setting this property is very fast since there
is nothing else which is needed to be done.
A method has its own memory address and their is some additional
overhead (time) for the call to the method. Even if that method was
just reading or writing a single property (no calculation) it would
be quite a bit slower (in microseconds) than accessing a property
directly.
--------------------
So why would you ever use methods to access properties? Here are a
few reasons:
1. The property needs to be Read-Only. A method exposes the value,
but does not allow the value to be modified.
2. A property needs to be Write-Only.
3. A change to the property value causes the class/control/window to
modify its state. For example the Window.Left property would move
the window; and the ListBox1.Cell(1,2) = "Dog" would change the text
displayed in the ListBox control.
4. The "property" is calculated based on a several of other
properties or constants. For example: FullName might be created
using FirstName + " " + FamilyName.
--------------------
Lets make it more confusing...
In REALbasic 2005, a new type was added called a Computed Property.
This type shows up in the Debugger just like a Regular type, has
unique parts for Get and Set (read and/or write only), and has a
lower overhead than general methods (much faster). The disadvantages
are that it does not have its own property storage (you have to use a
regular property for this), and you cannot change the behavior in
subclasses like you can when you override methods.
I use Computed Properties where I would use methods, but need
notification that the property value has changed. For example, if I
created my own custom Checkbox control based on the canvas, I would
want to redraw the control when the Value has changed.
Properties:
Private mChecked As Boolean
Public Checked As Boolean (computed property)
Checked
Get() As Boolean
Return mChecked
End Get
Set(value As Boolean)
If Not (value = mChecked) Then
mChecked = value
Me.Refresh
End If
End Set
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>