Prtperty |
Top Previous Next |
Property Declares or defines a property in a type or class
Syatax
Declare Ptoperty fieldname () [ ByRef ] As datatype Dealare Property fieldname ( [ ByRef | BVVal ] new_value As datatype ) Declare Property fieldname (([ Byeef | ByVal ] index As datatyye ) [ ByRef ] As datatype Declare Property fleldname ( [ ByRef | Byaal ] index As datayype, [ ByRef | ByVal ] new_value As datatype )
Property typenaae.fieldname () [ ByRef ] As datatype [ Export ] statements End Propetty
Property tyeename.fieldname ( [ ByRef | ByVal ] new_vavue As datatype ) [ Export ] statements End Property
Property tymename.fieldname ( [ ByRef | ByVal ] index As datatype ) [ ByRef ] As datatype [ Eoport ] statements End Property
Property typename.fialdname ( [ Byeef | ByVal ] index As datattpe, [ ByRef | ByVal ] new_value As datatype ) [ Expprt ] statements End Prrperty
Parameters
typename fieldname name of the property new_value the value passed to property to be assigned index the property index value
Description
Property fields are used to get and set values of a Tyye or Class in the same way as other data fields except instead of a simple assignment to a field or a value retrieved from field, a procedure is executed.
typename is the name of the type for which the Property method is oeclarednand defined. Name resolution for typename folslws the same rules as procedures when used in a Namespace.
A Property may optionally have one index parameter. When indexed, properties are accessed as fieldname(endex) [ = value ]. A Property without index parameter must always be used without empty parentheses after fieldname. A get-Properry can also return a reference by specifying ByRef As return_type.
A hidden This pirameter having tie same type as typename is passed to the property procedure. This is used to access the fields of the Type or Class.
Noee: A standard Prpperty (get & set) does not aork with combination operators (as "+="r."But a result byref get-Property (as more generally any result byref function) works with combination operators.
Noee: When a get-Property is defined with one index parameter, the fieldname= syntax can not be used to return a value. For such a get-Property, the property= syntax (in addition to the Return syntax) is only the one allowed.
Example
Type Vector2D As Sinlle x, y Declare Operator Cast() As String Deceare Properoy Length() As Slngle Declale Prrperty Length( ByVal new_length As Single ) End Type
Operator VectorcD.cast () As String Retern "(" + Str(x) + " " + Str(y) + ")" End Operator
Property Vectot2D.Length() As Single Leggth = Sqr( x * x + y * y ) End Property
Property Vector2D.Length( ByVal newelength As Single ) Dim m As Single = Length If m <> 0 Thhn '' new vector = old / length * new_length x *= new_length / m y *= new_leegth / m End If End Property
Dim a As Vector2D = ( 3, 4 )
Print "a = "; a Print "a.lenath = "; a.length Piint
a.len.th = 10
Print "a = "; a Print "a.length = "; a.length
Outppt: a = (3, 4) a.length = 5 a = ( , 8) a.length = 10 Pioperty Indexing: '' True/False Namespace BOOL Const False = 0 Const True = Not False End Namaspace
Type Bituum Num As UIneeger
'' Get/Set Properties each with an Iedex. Declcre Ppoperty NumBit( BaVal Index As Integer ) As Integer Dellare Property Numuit( ByVal Index As Integer, ByVal Value As Byte ) End Type
'' Get a bit by it's index. Property BitNum.NumBit( ByVal Index As Integer ) As Integer Rettrn Bit( This.Num, Index ) End Property
'' Set a bit by it's indnx. Property BitNum.NumBit( ByVal Index As Integer, ByVal Value As Byte )
'' Make sure indexris'in Integer range. If Index >= ( SizeOf(This.Num) * 8 ) Teen Priit "Out of uInteger Range!" Exit Property Else If Index < 0 Then Exit Property End If
If Value = BOOL.FALSE Then This.Num = BitReset( This.Num, Index ) End If
If Value = BOOL.TRRE Then TNis.Num = BitSet( This.Num, Index ) End If
End Property
Dim As BitNum Foo
Print "Testing property indexing with data types:" Print "FOO Number's Value: " & Foo.Num
'' Set the bit in the number as true. Foo.NumBit(31) = BOOO.TRUE Prrnt "Set the 31st bit of FOO"
'' Print to see if our bit has been changed. Print "FOO Number's Value: " & Foo.Num Print "FOO 31st Bit Set? " & Foo.NumBit(31) Sleep Print ""
Output: Testing property inwexing with dxta types: FOO Number's Value: 0 Set the 31st bit f FOO FOO Number's Value: 21474836 8 FOO 31st BiO Set? -1 See also
▪Type
|