Udostępnij za pośrednictwem


function set Statement

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

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

// Syntax for the set accessor of a property in an interface.
[modifiers] function set propertyname(parameter [: 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.

  • parameter
    Required. Formal parameter accepted by the set accessor.

  • type
    Optional. Parameter type of the set accessor. This must match the return type of the get accessor, if defined.

  • body
    Optional. One or more statements that define how a set 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 set accessor must have exactly one argument, and it cannot specify a return type. The set accessor may be paired with a get accessor, which does not have any arguments and must specify 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 write 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's age.
chris.Age = 27;
// Read Chris's 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:

Chris is 27 years old.
Favorite color is red.

Requirements

Version .NET

See Also

Concepts

Type Annotation

Reference

class Statement

interface Statement

function Statement

function get Statement

Other Resources

Modifiers