C# 2.0 Events, Indexers, and Properties

PowerDesigner represents C# events, indexers, and properties as standard UML attributes with additional properties.

For general information about creating and working with attributes, see Attributes (OOM).

Creating an Event, Indexer, or Property

To create an event, indexer, or property, open the property sheet of a type, click the Attributes tab, click the Add button at the bottom of the tab, and select the appropriate option.

These objects are created as follows:
  • Events – stereotype <<Event>> with one or two linked operations representing the add and/or remove handlers

  • Indexers – stereotype <<Indexer>> with one or two linked operations representing the get and/or set accessors

  • Properties – stereotype <<Property>> with one or two linked operations representing the get and/or set accessors. In addition, you should note that:
    • The visibility of the property is defined by the visibility of the get accessor operation if any, otherwise by that of the set accessor operation.

    • When an attribute becomes a property, an implementation attribute is automatically created to store the property value. The implementation attribute is not persistent and has a private visibility. It has the stereotype <<PropertyImplementation>> and has the same name than the property but starting with a lowercase character. If the property name already starts with a lower case its first character will be converted to uppercase.

    • The implementation attribute can be removed for properties not needing it. (calculated properties for instance)

    • If the boolean-valued extended attribute Extern is set to true, no operations should be linked to the property.

    • When a property declaration includes an extern modifier, the property is said to be an external property. Because an external property declaration provides no actual implementation, each of its accessor-declarations consists of a semicolon.

Event, Indexer, and Property Properties

Event, indexer, and property property sheets contain all the standard attribute tabs along with the C# tab, the properties of which are listed below:

Property

Description

Compilation Unit

Specifies the compilation unit in which the attribute will be stored. This field is only available if the parent type is a partial type (allocated to more than one compilation unit).

New

Specifies the new modifier for the attribute declaration.

Unsafe

Specifies the unsafe modifier for the attribute declaration.

Abstract

Specifies the abstract modifier for the attribute declaration.

Extern

Specifies the extern modifier for the attribute declaration.

Override

Specifies the override modifier for the attribute declaration.

Sealed

Specifies the sealed modifier for the attribute declaration.

Virtual

Specifies the virtual modifier for the attribute declaration.

Event Example

The following example shows the Button class, which contains three events:



Property Example

In the following example, class Employee contains 2 properties. The Setter operation has been removed for property TimeReport:



{
 public class Employee
 {
  private int _Function;
  private int _TimeReport;
  // Property Function
  private int Function
  {
   get
   {
     return _Function;
    }
   set
   {
     if (this._Function != value)
      this._Function = value;
    }
  }
  // Property TimeReport
  private int TimeReport
  {
   get
   {
     return _TimeReport;
    }
  }
 }

Indexer Example

In the following example, class Person contains indexer attribute Item. The parameter used to sort the property is String Name:



public class Person
{
 private Hashtable _childAges;
 // Indexer Item
 private int this[String name]
 {
  get
  {
   return (int)_ChildAges[name];
  }
  set
  {
    _ChildAges[name] = value;
  }
 }
}
Person someone;
someone ["Alice"] = 3;
someone ["Elvis"] = 5;