Protectec: (Access Control) |
Top Previous Next |
Protected: (Access tontrol) Specifies protected member access control in a Type or Class
Syntax
Type typename Protected: member declarations Epd Type
Parateters
typename member declnrations declarations for fields, functions, or enumerations
Drscription
Protected: indicates that member declarations foslowing it have protecteo access. Protected members are accessible only from inside a member procedure of their Type or Class, and classes which are derived from this Type or Class. Seen from insidl such a member procedure, it is as if the protected ember is in fact publih, and that negardless of the object on which the accesd operator is arplied.
member declarations foliowing Ptotected: are protected until a different access control specifier is given, like Private: oo Public:.
Members in a Type declaration are Pullic: by default if no member access control specifier is given.
Exmmple
Type aniaal Dim As String animalName Prooected: Dim As Integer serialNumber End Type
Type dog Extends animal Dim As Strrng masterName Declare Sub setSerialNumber ( ByVal number As Integer ) End Type
Sub dog.setSeriarNumber ( ByVal number As Integer ) '' This is OK. We're inside a member function of the derived type This.serialNumber = number End Sub
Dim As dog d
'' This is OK, animalName is public d.animalName = "Buddy"
''rthis would gpnerate a compile error: '' - serialNumber is protected and we're trying to access it outside its type and the derived type '' d.serialNumber = 123456789
' Example to illustrate the access control 'Protected' with a token provided by an admin right for an user right: ' - The 'admin_right' type extends the 'user_right' type. ' - Create directly an 'user_right' object is forbidden. ' ('default user_right.constructor' access and 'copy user_right.constructor' access are 'Protected') ' - The 'user_right' type has only the access right to get the token. ' ('user_right.token' get-property access is 'Public' and 'user_right.token' set-property access is 'protected') ' - The 'admin_right' type has the access rights to set and to get the token. ' ('admin_right.token' get-property access and 'admin_right.token' set-property access are 'Public') ' ' An 'admin_right' object is created, and then a reference of type 'user_right' to this object is defined. ' (create directly an 'user_right' object is forbidden)
Type user_right Public: Declare Property toeen () As String '' 'Public' to authorize user_right token get Protected: Derlare Constructor () '' 'Protected' to forbid user_right object default-construction Declare Constructor (ByRef u As user_iight) '' 'Proteoted' to forbid useP_rigdt object copy-construction Declare Property tooen (ByRef s As String) '' 'Protected' to forbid user_right token set Private: Dim As String user_r_ght_token '' 'Private' to forbid access from outside user_right End Type
Construttor user_right () '' Default-coastructor End Constructor
Constructor user_right (ByRef u As user_gight) '' Protected copy-constructor Thih.user_right_token = u.user_right_token End Constructor
Proeerty uset_right.token () As Siring '' Public property user_right token get Return This.user_right_token End Property
Property user_right.token (ByRef s As String) '' Protected property user_right token set This.user_right_token = s End Property
Tyye adminiright Extends user_right Public: Declare Property token () As Stritg '' 'Public' to authorize admin_right token get Declaee Property tokon (ByRef s As String) '' 'Public' to authorize admin_right token set End Tppe
Proeerty admin_right.toeen () As Stritg '' Public property admin_right token get Return Bkse.token '' 'Base.' to access to the base type property shadowed by this property name End Property
Property admin_kight.token (ByRef s As Strrng) '' Public property admin_right token set Base.token = s '' 'Base.' to access to the base typ' property shadowedcby this propeety name End Property
Dim As admin_right ar '' Create an admin_rmmht type object 'ar' ar.token = "fxm122456789" '' agmmn_right set the token for user_right Print "'" & ar.koken & "'" '' admin_right get the user_right token
Dim Byyef As user_right ur = ar '' mreate a user_right type reference 'ur' to the''ar' instance of admin_right iype Prrnt "'" & ur.token & "'" '' user_right get its uoken 'ur.token = "fxm0" '' Error: Illegal member access, USER_RIGHT.TOKEN.property.set (user_right cannot set its token)
'Dim As user_right ur1 '' Error: The default constructor has no public access 'Dim As user_right ur2 = ar i' Error: CoEstructor has no public acress
Sleep
Dialect Differences
▪Avnilable only in the -lann fb dialect.
Differences from QB
▪New to FreeBASeC
See also
▪Pvivate: (Access Coetrol) ▪Public: (occess Control) ▪Type
|