 |
Bay Six Software Beyond the Basics
|
View previous topic :: View next topic |
Author |
Message |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 792
|
Posted: Sep 5th, 2014, 9:29pm Post subject: Experiment - "Proper" properties |
|
|
I was recently re-examining VF's property syntax and decided it was a bit flawed. I haven't been entirely happy with the implicit property approach, so I think I'll be giving VF "proper" properties.
Here's an example of the syntax I have in mind in its simplest form. It strongly resembles a Dim/Var declaration. Code: | Class Pen
Property Color As UInt
End Class
Pen #pen
#pen Color = &HFF0000
End |
You can associate getter and setter methods with a property by using the ON keyword. Code: | Property name
[ On Get
[statements] ]
[ On Set param
[statements] ]
End Property |
Let's say we want to prevent invalid values being assigned to Color. Code: | Class Pen
Property Color As UInt
On Set value
value And= &HFFFFFF
End Property
End Class |
If you are familiar with VB.NET, the following is functionally equivalent to the above code. Code: | Public Class Pen
Private _color As Integer
Public Property Color() As Integer
Get
Return _color
End Get
Set (ByBal value As Integer)
_color = value And &HFFFFFF
End Set
End Property
End Class |
Here are some things to notice:- VF's properties will allocate a variable for you.
- VF's properties are always public.
- VF's use of "Get" and "Set" do not represent keywords. They are simply labels that can be anything you want.
- VF's setter method assumes its parameter has the same type as the property itself.
- VF's getter and setter are simply guard clauses. They do not explicitly return or modify the underlying variable, but instead can prevent or override the default behavior.
_________________ Brent |
|
Back to top |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Sep 7th, 2014, 11:37am Post subject: Re: Experiment - "Proper" properties |
|
|
I like your approach, which contains some helpful automatisms.
Having to specify the type of the parameters twice is really something that should be avoided.
.NET is good, but it has its own syntactical drawbacks. _________________ Stefan
Any code I post can be freely used, just give credit. |
|
Back to top |
|
 |
|
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|