Поделиться через


Properties Overview

A component should define properties instead of public fields, because visual designers such as Visual Studio .NET display properties, but not fields, in the property browser. (Other compelling reasons to define properties are listed at the end of this topic).

Properties are like smart fields. A property generally has a private data member accompanied by accessor functions and is accessed syntactically as a field of a class. (Although properties can have different access levels, the discussion here focuses on the more common case of public access.) Because properties have been available in several editions of Visual Basic, Visual Basic programmers might wish to skip this topic.

A property definition generally consists of the following two pieces:

  • Definition of a private data member.

    private int number = 0;
    [Visual Basic]
    Private number As Integer = 0
    
  • Definition of a public property using the property declaration syntax. This syntax associates the private data member with a public property through get and set accessor functions.

    public int MyNumber 
    {
    // Retrieves the number data member.
    get 
       { 
          return number; 
       }
    // Assigns to the number data member.
    set 
       { 
          number = value;
       }
    }
    [Visual Basic]
    Public Property MyNumber As Integer
       ' Retrieves number.
       Get 
          Return number
       End Get
       ' Assigns to number.
       Set 
          number = value
       End Set
    End Property
    

The term value is a keyword in the syntax for the property definition. The variable value is assigned to the property in the calling code. The type of value must be the same as the declared type of the property to which it is assigned.

While a property definition generally includes a private data member, this is not required. The get accessor could return a value without accessing a private data member. One example is a property whose get method returns the system time. Properties enable data hiding, the accessor methods hide the implementation of the property.

There are some differences in the property syntax among different programming languages. For example, the term property is not a keyword in C#, but it is a keyword in Visual Basic .NET. For language-specific information, refer to the documentation for that language,

The following example defines a property named MyNumber in the class SimpleProperty and accesses MyNumber from the class UsesSimpleProperty.

public class SimpleProperty 
{
   private int number = 0;
   public int MyNumber 
   {
   // Retrieves the data member number.
   get 
      { 
         return number; 
      }
   // Assigns to the data member number.
   set 
      { 
         number = value;
      }
   } 
// Other members.
}
public class UsesSimpleProperty
{
   public static void Main()
   {
      SimpleProperty example = new SimpleProperty();
// Sets the property.
      example.MyNumber = 5;
// Gets the property.
      int anumber = example.MyNumber;
   }
}
[Visual Basic]
Public Class SimpleProperty 
    Private number As Integer = 0

    Public Property MyNumber As Integer
        ' Retrieves number.
        Get 
            Return number
        End Get
        ' Assigns to number.
        Set 
            number = value
        End Set
    End Property    

' Other members...
End Class

Public Class UsesSimpleProperty
    Public Shared Sub Main()
        Dim example As New SimpleProperty()
        ' Sets the property.
        example.MyNumber = 5
        ' Gets the property.
        Dim anumber As Integer = example.MyNumber
    End Sub
End Class

The get and set methods are generally no different from other methods. They can perform any program logic, throw exceptions, be overridden, and 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.

The type of a property can be a primitive type, a collection of primitive types, a user-defined type, or a collection of user-defined types. For all primitive types, the .NET Framework provides type converters that implement string-to-value conversions. For details, see Generalized Type Conversion. When a type converter is available for a property, it can be displayed in the property browser in the designer. If you define custom properties and want the property browser to display them, you must implement custom type converters.

When the data type of a property is an enumeration, a development environment such as Microsoft Visual Studio .NET will display the property as a drop-down list in the Properties window. If the data type of a property is a class that has properties, those properties are called subproperties of the defining property. In the Properties window in Visual Studio .NET, a user can expand a property to display its subproperties.

It is important to add attributes to properties so that they are displayed appropriately in the property browser at design time. For details, see Design-Time Attributes for Components.

You should expose properties instead of public fields from your components, because properties can be versioned, they allow data hiding, and the accessor methods can execute additional logic. Generally, because of just-in-time optimizations, properties are no more expensive than fields.

See Also

Design-Time Attributes for Components