This documentation is archived and is not being maintained.

Property Implementation in Components

Visual Studio .NET 2003

Properties allow your component to store, manipulate, and retain data that it requires to run. They are similar to member variables, in that they provide storage for data, but properties are much more robust. Properties are accessed by special methods; the Get and Set accessors, which allow property values to be validated, additional code to be executed, or other tasks to be performed when the property is set or retrieved. The following example shows a hypothetical property that includes validation code:

' Visual Basic
' This is the private member variable that stores the value for the 
' property.
Private pMyProperty as Integer
' This is the property block.
Public Property MyProperty() as Integer
   Get
      'Returns the value if pMyProperty.
      Return pMyProperty
   End Get
   Set (Value As Integer)
      ' Checks if a valid value is being assigned to the property.
      If Value > 5 Then
         Messagebox.Show( _
            "Invalid value for the property. Please try again")
      Else
         pMyProperty = Value
      End If
   End Set
End Property

// C#
// This is the private member variable that stores the value for the 
// property.
int m_MyProperty;

// This is the property block.
public int MyProperty
{
   // Returns the value of m_MyProperty.
   get
   {
      return m_MyProperty;
   }

   set
   {
      // Checks if a valid value is being 
      // assigned to the property.
      if (value > 5)
      {
         MessageBox.Show(
         "Invalid value for the property. Please try again");
      } 
      else 
      {
         m_MyProperty = value;
      }
   }
}

Read-Only Properties

Properties are by default read-write, meaning the values stored in them can be both retrieved and set. Occasionally, you may wish to create a property that cannot be set, but can only be retrieved. These are called read-only properties. Read-only properties do not have a Set accessor, and their values cannot be changed during run time. An example of a read-only property is shown below: (In C#, a read-only property is created by defining only the get method.)

' Visual Basic
' This member variable will hold the value of the property. 
Private ReadOnly pLength as Integer
Public ReadOnly Property Length() as Integer
   Get
      Return pLength
   End Get
End Property

// C#
// This member variable will hold the value of the property. 
private int pLength;
                        
public int Length
{
   get 
   {
      return pLength;
   }
}

The value of a read-only variable (pLength in the above example) can only be set in the constructor of the component or when the member variable is initialized.

Write-Only Properties

Under rare circumstances, you may wish to create a property that can only be set, but the value cannot be retrieved. You might do this in a component that alters global properties in response to an internationalization value, for example. In such a component, different methods would be executed based on the value to which the property is set. These write-only properties, as they are called, do not contain a Get accessor, and cannot be read during execution of the program. An example is shown below:

Private pLanguage as myenumLanguage
Public WriteOnly Property Language() as myenumLanguage
   Set (Value As myenumLanguage)
      pLanguage = Value
      Select Case Value
         Case myenumLanguage.English
            ' Code to implement English.
         Case myenumLanguage.Icelandic
            ' Code to implement Icelandic.
         Case myenumLanguage.Tagalog
            ' Code to implement Tagalog.
      End Select
   End Set
End Property

// C#
myenumLanguage m_language;
public myenumLanguage Language
{
   set
   {
      m_language = value;
      switch (value) 
      {
         case myenumLanguage.English :
            // Code to implement English.
            break;
         case myenumLanguage.Icelandic :
            // Code to implement Icelandic.
            break;
         case myenumLanguage.Tagalog :
            // Code to implement Tagalog.
            break;
         default :
            break;
      }
   }
}

The preceding example creates a write-only property that executes different sets of code based upon the value of a property when it is written. There is no way to retrieve the property once it is set, and unless you have a specific reason for making the value of the property inaccessible, it is recommended that you use a read-write property for these constructions.

See Also

Method Implementation in Components | Event Implementation for Components and Custom Controls with Visual Basic | Exposing Properties of Contained Objects

Show: