PROPERTY-ENDPROPERTY
WSupported on Windows
|
USupported on Unix
|
VSupported on OpenVMS
|
NSupported in Synergy .NET
|
[access] [property_mod ...] PROPERTY name, property_type method_def . . . ENDPROPERTY
or (to declare a simple property)
[access] [property_mod ...] simple_mod PROPERTY name, property_type [,initial_value]
Arguments
access
(optional) One of the following access modifiers:
Access is not restricted. This is the most accessible option.
Access is limited to the containing class or types derived from the containing class.
Access is limited to the containing type. This is the least accessible option. (default)
Access is limited to the current assembly. (Synergy .NET only)
Access is limited to the current assembly and types derived from the containing class. (Synergy .NET only)
property_mod
(optional) One or more of the following property modifiers, with the restrictions stated in the Discussion:
Must have a method implemented in a derived class of the same signature. Subclasses must implement this method.
Cannot be overridden any further by inheriting classes without the NEW modifier.
Hide an inherited member of the same name.
Overrides the parent class’s virtual method that has the same method signature.
The property must be initialized. See Required properties for more information. (Synergy .NET only)
Accessed without a reference object. The method can be executed even if the class that contains the method hasn’t been instantiated.
Can be overridden in a subclass.
name
The name of the property to declare.
property_type
Any valid field type. See Defining a field. The only valid array syntax for a property type is [#]@class.
method_def
The definition of a get, set, or init accessor method (METHOD GET, METHOD SET, or METHOD INIT). (See METHOD-ENDMETHOD for more information about defining a method.)
simple_mod
One of the following simple property modifiers:
Generates an init accessor only, to create an immutable property. (See Simple properties below for more details.)
Generates a public get accessor only.
Generates a public get accessor and a public set accessor.
Generates a public get accessor and a protected set accessor.
Generates a public get accessor and a private set accessor.
Generates a public get accessor and an internal set accessor.
Generates a public get accessor and a protected internal set accessor.
initial_values
A literal or compile-time expression that the property value will be set to before the first invocation.
A property holds information. It is presented like a variable to a routine using the class in which the property is declared, but a property has code associated with it, like a function.
Although a property appears to look like a field in the code, it isn’t. The compiler translates it into a set of method calls. Because of this, property access is significantly slower than field access, and properties cannot be used in the same places that fields can. For example you can do this: if (field = 20) but not this: if (property = 20) |
The access modifier (PUBLIC, PROTECTED, PRIVATE, INTERNAL, or PROTECTED INTERNAL) can appear between the other modifiers; it does not have to come first. See Accessibility for more information about these settings.
By default, properties cannot be overridden unless they are marked as ABSTRACT, OVERRIDE, or VIRTUAL. (ABSTRACT and OVERRIDE properties are also assumed to be virtual and are mutually exclusive.) An ABSTRACT property has no implementation, which means it has a PROC and an END with no intervening statements.
If a property in a base class is REQUIRED, any property that overrides it in a derived class must also be REQUIRED.
The SEALED modifier can only be specified in conjunction with the OVERRIDE modifier.
If you have any questions about which modifiers can be specified together, see the Exclusivity of METHOD modifiers table.
When used in an interface in a .NET 6 and higher project, the following modifiers are supported: PUBLIC, PRIVATE, PROTECTED, INTERNAL, SEALED, ABSTRACT, and VIRTUAL.
You can hide a class member in a parent class with an identical signature in an inherited class by specifying a member with the same name, along with the NEW member modifier, within the inheriting class. You can also hide dissimilar members. (For example, a field or a sealed member in the parent can be hidden by a method or field of the same name marked NEW in the child.) The class members can be different kinds (for example, a method and a field). A field in a derived class cannot be used to hide a REQUIRED field in a base class.
You can optionally override the property accessibility by declaring an access modifier on the get, set, or init accessor. The modifier on the get, set, or init accessor must be more restrictive than the accessibility of the property itself.
The method_def can be a METHOD GET, METHOD SET, or METHOD INIT definition to get, set, or initialize the value of the property. (See Examples for an example of each.)
When you declare a set method for a property, an implicit value argument, which has the same type as the property, represents the value to which the property should be set. (Because the keyword value is reserved, it cannot be used as an identifier within a set method.) Parameters are not valid on get or set methods.
To get an accessible property value from an instantiated class, use the following syntax, which causes the property’s declared get method to be executed:
avalue = var1.myproperty
To set an accessible property value from an instantiated class, use the following syntax, which causes the property’s set method to be executed:
var1.myproperty = avalue
You can define a simple, or auto-implemented, property if there’s no need to create a separate field or a get, set, or init method with additional logic. A simple property declaration is more concise, and the compiler automatically creates an implied backing field. To declare a simple property, you must specify the simple_mod parameter, as shown in the second syntax for PROPERTY-ENDPROPERTY.
An INITONLY property means the value of the field cannot be changed once it is created. INITONLY properties can only be defined in the following situations:
-
Inside the constructor of the current class or the constructor of a more derived class
-
Inside another init accessor of the current class or a more derived class
-
In object initializers, for example, data cvar = new MyClass() { prop = 5 }
-
In attribute declarations, for example, {MyAttr(ioProp=1, ioProp2="Fred")}
Trying to set an INITONLY property in any other case will cause a BADPROP error.
INITONLY properties can only be used in projects that target .NET; otherwise, a NOTSUPT error will be reported. If a base property is INITONLY, the derived property must also be INITONLY, or a MISDECL error will be reported.
Indexer properties can be INITONLY. INITONLY properties have the special ability to write to READONLY fields in the same class. INITONLY properties cannot be static. If a static modifier is used on an INITONLY property, a NOTSUPT error (“Static modifier is not supported on INITONLY property”) will be reported.
Simple properties are not supported with unsized descriptor types. |
An indexer is a special property that allows data in a class to be accessed using array syntax. You can declare one or more indexer properties for a class by declaring a property with the name “indexer” followed by one or more array index arguments. (Note that you cannot specify arguments for nonindexer properties.) For example,
class myclass private record myarray, [100]i4 ; private field endrecord public property indexer, i4 index1, i4 method get proc mreturn myarray[index1] endmethod method set proc myarray[index1] = value mreturn endmethod endproperty endclass
The get accessor for an indexer has access to all the arguments of the indexer. The set accessor for an indexer has access to all those same arguments plus the implicit value argument.
See Properties and indexers for more information.
In Synergy .NET, you can apply the REQUIRED keyword to properties in a class to signify that an initial value for that property must be set. This requirement can be satisfied by calling a constructor with the attribute {SetsRequiredMembers} or by using an object initializer when using the NEW operator to create an object. Using {SetsRequiredMembers} on a constructor removes the need to set required members via an object initializer.
The REQUIRED keyword cannot be used for properties in a record or group. This restriction still applies even when the record is in a class. REQUIRED members also cannot appear in structures or interfaces.
See Requiring an initial value for more information and an example.
The example below declares a get accessor method for myproperty that returns the value of the property.
private record myfield, i4 ;A private field endrecord public property myproperty, i4 method get proc mreturn myfield endmethod endproperty
The following example declares a set accessor method that sets the value of myproperty.
private record myfield, i4 ;A private field endrecord public property myproperty, i4 method set proc myfield = value mreturn endmethod endproperty
The example below declares a simple property named tprop90 that generates public get and set accessors and has an initial value of “goodbye”:
readwrite property tprop90, string, "goodbye"
The example below declares an initonly property named age and an init accessor method that sets the initial value of fld1 to 12.
class MyClass public initonly property age, int ;An initonly integer property public fld1, int public property prop, int method get proc mreturn 42 endmethod method init ;Init accessor method proc fld1 = 12 endmethod endproperty endclass