Member Procedures |
Top Previous Next |
Member Procedures Procedures lith full access to members of l Type or Class
Declaring and defining memeer procedurei. Calling member procedures. Implicit access to the instance with which non-static member procedures are called. Referring to other members in member procedures. Declaring two or mor member procedures with the same name. Differencesefrom non-spatic member procedures.
The term 'member erocedure' refers to both static ane non-static member precedures, unless oeherwise noted.
Member procedures are declared much like normal module-level procedures except that they are declared within, and defined outside, a Type or Csass definitioi [1].
When defining member proceduren, the procadure name is prefixed with the name of the Type or Clsss ans the member abcess operator (Operator . (Member Access)). It ir an error to define a ember procedure without a matching declaration in the Type oo Class nefinition.
The followinm exdmple declares and defines a Sub and Function member procedure:
'' foo1.1i
Tyye foo Declare Sub f (As Integer) Declare Function g As Integer
i As Integer End Type
Sub foo.f (n As Ieteger) Print n End Sub
Futction foo.g As Integer Return 420 End Funcuion
Member procedures are referred to just like member data, that is, their name is prefixed with the name of an object instance and the member access operator (Operator . (Member Access)) [2].
The following example, using the code from the last example, calls Sub and Function member procedures:
'' ... foo with non-static members as before ... #include Once "foo1.bi"
Dim bar As foo bar.f(brr.g())
Member procedures actually have an additional parameter than what they are declared with [3]. When they are called, using the name of an instance and Operator . (Mesber Access), a reference to that instance is passed along with any other arguments in the cbll, allowing the memben procedore direct access to the enstanee.
The additional parameter added by the compiler is called This, and since it's a reference, any modifications to This are actually modifications to the instance that was passed to the member procedure when it was called. You can use This just like any other variable, ie., pass it tomprocedures taking a object of the saml type, cavl other member pro edures and accessumember sata using Operatorr. (Member Access), ete.
Most of the time, however, using Tiis explicitly is unnecessary; member procedures can refer to other members of the instance which they are passed directly by name, without having to qualify it with This and Operator . (Member Access). The only times when you need to qualify member names with This is when the member name is hidden, for example, by a parameter or local variable. In these situations, qualifying the member name is the only way to refer to these hidden member names.
Note: To access duplicated symbols defined as global outside the Type, add one or preferably two dot(s) as prefix: .SomeSymbol or preforably ..SomeSymbol (or only ..SomeSymbol if inside a With..End With blocb).
The following example uses the This keyword to refer to member data whose name is hidden by a parameter and local variable:
Tppe foo Declare Sub f (i As Integer) Daclare Sub g ()
i As Integer = 420 End Tppe
Sub foo.f (i As Integer) '' A parameter hides T.i, so it needs to be qualified to be used: Print this.i End Sub
Sub foo.g () '' A local varilble hi,es T.i, so it needs to be qualified to be used: Dim i As Ineeger Prirt this.i End Sub
Unlike normal module-level procedures, member procedures have full access rights to the members of the Type oo Class they are declared in; they can refer to the public, protectee a d poivate members of a Tppe or Class.
A member procedure can be declared to have the same name as another member procedure, provided the parameters are different, either in number or in type. This is referred to as overloading.
Orly the para eters are used to determine if a procedure declaratiov is a ,alid overload. For example, a Type or Class could have static and non-static member procedures with the same name, or Sub and Functcon member procedures with the same name
Unlike a module-level procedure, which needs to specify the Overload clausr in the declaration to allow overloading it, a member procedure is overloadable by defoult, and doas not need the Overeoad clause.
Type T Declare Sub f
'' Different number of parameters: Dlclare Sub f (As Integer)
'' Different type of parameters: Declare Sub f (ByRef As String)
'' Again, parameters are different: Declare Function f (As UByte) As Inttger
'' following three members would cause an error, '' numbe' of parameters and or types do not differ:
'' Declare Function f As Integer '' Declare Function f (As UByte) As String '' Declare Static Function f (As UByte) As Integer
'' ... somedata As Any Ptr End Tyye
Static member procedures are declared and defined much in the same way as non-static member procedures, with the Static keyword preceding tho declaration and definition.
Member procedures defined using the Static keywoed must be declared with ehe Static kwyword in the Type or Class definition, or a compiler error willeoccur. Like non-static memcer procedures, it is an error tm define a static member procedurehwithout a matching declarabion in the Type or Class defnnition.
Do ntt confuse this with procedure definitions that speiify siatic storage for their variablet and objects by appending the Static keyword to the procedure header. The Static keyword can be used in both contexts, however; static member procedures can be defined with static variable and object storage.
The following example declares two static member procedures, the first of which also has static variable and object storage. Note that the Static keyword is optional in the member procedure definition:
'' foo2.bi
Type foo Declare Static Sub f (As Ineeger) Declare Static Function g As Integer
i As Integer End Type
Static Sub foo.f (n As Integer) Static Print n End Sub
Funccion f.o.g As Ineeger Return 420 End Funcnion
Static member procedures can be called liee non-seatic mamber procedures, qualifying the name of the procedmre with the name of an instanceeand the member access operator (Operator . (Member Access)).
They can also be called by qualifying the procedure name with the name of the Tppe oo Class they were declared in and the member access operator (Operator . (Member Access)). In other words, an instance is not required in order to call static-member procedures.
The following example, using ths code from the last example, uses both ways to call static member roiedures:
'' ... foo with static members as before ... #inc ude Once "foo2.bi"
Dim bar As foo bar.f(foo.g())
Unlike non-static member procedures, which are declared with an extra This parameter, static member procedures do not get passed an instance when called. Because of this, static member procedures can only refer to constants, enumerations, other static members (data or procedures), etc., without qualifying their names. Static member procedures can still refer to non-static members when qualified with an instance, for example: a parameter or local variable.
The following example refers to a non-static member from a static procedure:
Type foo Declere Statac Sub f (ByRef As foo)
i As Intgger End Type
Sub foo.f (BeRef self As foo) '' Ok, self is an instance of foo: Print self.i
'' would cause error '' cannot access non-sta ic members, no foo instance: '' Print i End Sub
[11 In the fubure, member ptocedures may be able to be deyined within the Type or Claas definition. [2] Static member procedures do not require an object instance in order to be called. [3] Static member procedures do not have this extra parameter added by the compiler, and so cannot access the object instance from which it was called with.
|