function get Statement

Declares the accessors for a new property in a class or an interface. Often function get will appear in conjunction with a function set to allow read/write access to a property.

// Syntax for the get accessor for a property in a class.
 [modifiers] function get propertyname() [: type] {
   [body]
}

// Syntax for the get accessor for a property in an interface.
[modifiers] function get propertyname() [: type]

Arguments

  • modifiers
    Optional. Modifiers that control the visibility and behavior of the property.

  • propertyname
    Required. Name of property being created. Must be unique within the class except the same propertyname can be used with both get and set accessors to identify a property than can be read from and written to.

  • type
    Optional. Return type of the get accessor. This must match the parameter type of the set accessor, if defined.

  • body
    Optional. One or more statements that define how a get accessor operates.

Remarks

The properties of an object are accessed in much the same way as a field is accessed, except that properties allow for more control over the values that are stored in and returned from the object. Properties can be read-only, write-only, or read-write depending on the combination of get and set property accessors defined within the class. Properties are often used to help make sure that only appropriate values are stored in a private or protected field. You may not assign a value to a read-only property or read a value from a write-only property.

A get accessor, which must specify a return type, does not have any arguments. A get accessor may be paired with a set accessor, which has one argument and does not have a return type. If both accessors are used for a property, the return type of the get accessor must match the argument type of the set accessor.

A property may have either a get accessor or set accessor or both. Only the get accessor (or set accessor if there is no get accessor) of a property may have custom attributes that apply to the property as a whole. Both the get and set accessors can have modifiers and custom attributes that apply to the individual accessor. Property accessors cannot be overloaded, but they can be hidden or overridden.

Properties can be specified in the definition of an interface, but no implementation can be given in the interface.

Example

The following example shows several property declarations. An Age property is defined as read from and written to. A read-only FavoriteColor property is also defined.

class CPerson {
   // These variables are not accessible from outside the class.
   private var privateAge : int;
   private var privateFavoriteColor : String;

   // Set the initial favorite color with the constructor.
   function CPerson(inputFavoriteColor : String) {
      privateAge = 0;
      privateFavoriteColor = inputFavoriteColor;
   }

   // Define an accessor to get the age.
   function get Age() : int {
      return privateAge;
   }
   // Define an accessor to set the age, since ages change.
   function set Age(inputAge : int) {
      privateAge = inputAge;
   }

   // Define an accessor to get the favorite color.
   function get FavoriteColor() : String {
      return privateFavoriteColor;
   }
   // No accessor to set the favorite color, making it read only.
   // This assumes that favorite colors never change.
}

var chris: CPerson = new CPerson("red");

// Set Chris age.
chris.Age = 27;
// Read chris age.
print("Chris is " + chris.Age + " years old.");

// FavoriteColor can be read from, but not written to.
print("Favorite color is " + chris.FavoriteColor + ".");

When this program is run, it displays the following:

Chrisis 27 years old.
Favorite color is red.

Requirements

Version .NET

See Also

Reference

class Statement

interface Statement

function Statement

function set Statement

Concepts

Type Annotation

Other Resources

Modifiers