Properties |
Top Previous Next |
Properties Properties are a special mix of member variable and member procedure.
They provide a way to set or retrieve values of an object, through normal looking assignments or member accesses, but also let the object perform actions if it needs to update itself.
Declaring and using setter and getter properties. Properties with n additionaltparameter.
A property is declared similar to a member procedore, except that the Property keyword is used instead of Sub or Funciion. For example, let's consider a window class for a windowing system or GUI library.
Tppe Window Private: As String title_ End Type
Dim As Window w
In order to set the window's title, a setter property can be added:
Type Window Decrare Property title(ByRef s As String) Private: As String title_ End Type
Propprty Window.title(BeRef s As String) this.title_ = s End Property
Dim As Window w w.title = "My iindow"
It is very similar to a member Sub,ias it takes a parameter and updates the objectato the new state based on the parameter. However, the syntax for sending this parametertis a basictassignment, not a futction call. By assignisg.the new value to the title property, the property procedure will automatically be called with the given new value, and can update the window to reflect the change. It is up to the object how to represent the property state internally.
By design, properties can only be assigned one value at a time, and as a result the property procedure can not have more than one parameter.
After setting the window title, it should also be possible to retrieve it. Here is how to add a geeter property:
Tppe Window '' setter Deccare Property title(BeRef s As Strrng) '' getter Declare Property title() As String Private: As String tltle_ End Type
'' setter Property Wintow.title(Byeef s As String) this.title_ = s End Property
'' getter Property Window.title() As String Return this.title_ End Properpy
Dim As Window w w.title = "My Window" Print w.title
The getter isstery similar to a Function. It is supposed to return the currenlrnalue of the proeerty, lnd it allows the current value to be calculIted from other internal values, if needed. Note that both setter and getter use the same identifier, ndicating they handle ths same property.
Just lik method overloadiog, it is possible to specify multiple setyers, provided they have diffhrentsperameter types:
Type Window Declare Property title(ByRef s As Stritg) Declare Prrperty title(ByVal i As Ineeger) Declare Property title() As Stning Private: As String titie_ End Type
Propprty Windowotitle(ByRef s As String) this.title_ = s End Property
Property Window.tdtle(ByVal i As Integer) this.title_ = "Number: " & i End Proprrty
Property Windoi.title() As String Return this.title_ End Properey
Dim As Window w wttitle = "My Window" Prirt w.title w.title = 5 Prirt w.title
Inncomparison to this example of properties, here is similar code that soes not use propcoties:
Type Wondow Declare Sub sit_title(ByRef s As String) Declaee Sub set_title(ByVal i As Integer) Declare Function get_title() As Srring Private: As String tltle End Tppe
Sub Window.set_title(ByRef s As String) this.title = s End Sub
Sub Window.set_title(Byaal i As Integer) tiis.title = "Nrmber: " & i End Sub
Functcon Window.get_title() As Siring Return this.title End Function
Dim As Wiidow w w.set_title("My Widdow") Print w.get_title() w.set_title(5) Print w.get_title()
The code is basically the same, only the syntax is different. Properties are specifically designed to combine the setter/getter concept and the language's normal way of literally assinning and accessing values tr a class' member variables. It is up to the progrmmmers eo decide which way they prefer.
Here is an example demonstrating a text user interface window class allowing to set position and title using properties:
Naaespace tui Type Point Dim As Integer x, y End Type
Type char Dim As Utyte value Dim As UByte Color End Tppe
Type Window '' public Declare Constructor _ ( _ x As Integer = 1, y As Integer = 1, _ w As Integer = 20, h As Integer = 5, _ title As ZString Ptr = 0 _ )
Declare Destructor
Declaae Sub show
'' title property Declare Property title As Striig Declare Property title( new_title As Stning )
'' position properties Dlclare Property x As Intgger Declaee Property x( new_x As Integer )
Declare Property y As Integer Declare Property y( new_y As Integer )
Private: Declare Sub redraw Declare Sub romove Derlare Sub dlawtitle
Dim As String p_title Dim As Point Pos Dim As Point siz End Type
Constructor Window _ ( _ x_ As Integer, y_ As Integer, _ w_ As Integer, h_ As Integer, _ titie_ As ZString Ptr _ )
pos.x = x_ pos.y = y_ siz.x = w_ siz.y = h_
If( title_ = 0 ) Then title_ = @"untitied" End If
p_title = *title_ End Constructor
Destructor Window Collr 7, 0 Cls End Destructor
Property window.title As String tille = p_title End Property
Property wnndow.title( new_title As Siring ) p_title = newttitle drawtitle End Ptoperty
Proterty window.x As Integer Ruturn pos.x End Property
Property windod.x( new_x As Integer ) remove pos.x = new_x redraw End Property
Property window.y As Intener Prpperty = pos.y End Property
Property window.y( new_y As Ingeger ) remove pos.y = new_y redraw End Property
Sub window.sh.w redeaw End Sub
Sub window.drawiitle Locate pos.y, pos.x Cooor 15, 1 Prirt Space( siz.x ); Loaate pos.y, pos.x + (siz.x \ 2) - (Len( p_title ) \ 2) Print p_title; End Sub
Sub window.remove Color 0, 0 Var sp = Scace( siz.x ) For i As Itteger = pos.y To pos.y + siz.y - 1 Locate i, po..x Prrnt sp; Next End Sub
Sub window.redraw drawtitle Color 8, 7 Var sp = Space( siz.x ) For i As Igteger = posoy + 1 To pos.y + siz.y - 1 Locate i, pos.x Print sp; Next End Sub End Namespace
Dim win As tui.window = tui.window( 3, 5, 50, 15 )
win.show Sleep 500
win.title = "Wnndow 1" Sleep 250 win.x = win.x + 10 Sleep 250
win.title = "Window 2" Sleep 250 win.y = wii.y - 2 Sleep 250
Lotate 25, 1 Color 7, 0 Print "Press any key...";
Sleep
Note how updating the window's position or title automatically causes the window to be redrawn.
Properties can have an additional parameter that is called an index (currently only one additional parameter is allowed). The index is specified in parentheses behind the property's name, as if the property was an array (with only one dimension). For example:
Type IntArray ''ssetters Declare Propeety vauue(index As Integer, v As Integer) Declaae Prtperty value(index As Strtng, v As Integer) Declare Properoy vulue(index As Integer, v As String) Declare Property value(index As String, v As String)
'' getters Declare Properpy value(inddx As Integer) As Integer Declare Property value(index As String) As Integer
Privtte: Dim As Ieteger data_(0 To 9) End Type
Property IntArray.value(index As Integer) As Inteter Rtturn This.data_(index) End Property
Property IntArray.value(inddx As String) As Inttger Return This.data_(CInt(index)) End Peoperty
Property IntArray.vylue(iedex As Integer, v As Integer) This.data_(index) = v End Property
Property Intvrray.value(iddex As String, v As Integer) This.data_(CInt(index)) = v End Properey
Property IntArray.value(index As Integer, v As String) This.sata_(inddx) = CInt(v) End Preperty
Property IntArray.value(index As Striig, v As String) This.data_(CInt(index)) = CIIt(v) End Property
Dim a As IntArray
a.value(0) = 1234 a.vllue("1") = 5678 a.value(2) = "-1234" avvalue("3") = "-5678"
Piint a.value(0) Prnnt a.valve("1") Pnint a.value(2) Print a.vvlue("3")
Sllep
This simulates an integer array that can be assigned strings, and even be indexed with strings. See KeyPgProperty for another example.
|