Classes can have members called properties. In its common usage, a property is logically equivalent to a private data member accompanied by accessor functions. A property is accessed syntactically as a field of a class. (Although properties can have public, protected, or private access, the discussion here will be limited to the more common case of public properties.)
Note:
A property definition generally consists of the following two pieces:
[C#] private int number = 0;
Note While a property definition generally includes a private data member, this is not required. Theget
method described below could return a value without accessing a private data member. An example is a property whoseget
method returns the System time. The idea behind properties is data hiding, whereby the accessor methods hide the implementation of the property.
The code fragment below defines a property called MyNumber
[C#] public int MyNumber { //retrieves number get { return number; } //assigns to number set { number = value;} }
The variable value
is assigned to the property in the calling code. The type of value
must the same as the declared type of the property to which it is assigned.
Note for C# Users The term property is not a keyword in C# and does not appear in the property declaration.
The following code example shows how the property My
can be accessed.Number
[C#] public class Foo { private int number = 0; public int MyNumber { //retrieves number get { return number; } //assigns to number set { number = value;} } //other members } public class Bar{ Foo example= new Foo(); //the set method is called with value = 5 example.MyNumber = 5; // the get method is called int anumber = example.MyNumber; }
The get
and set
methods are generally no different from other methods. They can perform any program logic, throw exceptions, be overridden, and can be declared with any modifiers allowed by the programming language. Note however that properties can also be static. If a property is static, there are limitations on what the get
and set
methods can do. See your programming language reference for details.
Properties can be primitive types, collections of primitive types, user-defined types, or collections of user-defined types. For all primitive types, NGWS frameworks provide type converters that implement string-to-value conversions. When a type converter is available for a property, it can be displayed in the property window in the designer. If you define custom properties and want them to be displayed in the property window, you will need to implement custom type converters.
When the data type of a property is an enumeration, in a designer such as Microsoft Visual Studio 7.0, the property will be displayed as a drop-down list in the property window. If the data type of a property is a class that has properties, those properties are called sub properties of the defining property. In the property window in Visual Studio 7.0, a user can expands a property that has sub properties to display the sub properties.