Visual Basic 2005 Fields, Events, and Properties

PowerDesigner models Visual Basic 2005 fields, events, and properties as standard UML attributes with additional properties.

Creating a Field, Event, or Property

To create a field, event, 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.

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

Working with Events

Events are modeled as attributes with a stereotype of <<Event>>, and with one or two linked operations representing the add and/or remove handlers. You declare events within classes, structures, modules, and interfaces using the Event keyword, as in the following example:
Event AnEvent(ByVal EventNumber As Integer)

An event is like a message announcing that something important has occurred. The act of broadcasting the message is called raising the event.

Events must be raised within the scope where they are declared. For example, a derived class cannot raise events inherited from a base class.

Any object capable of raising an event is an event sender, also known as an event source. Forms, controls, and user-defined objects are examples of event senders.

Event handlers are procedures that are called when a corresponding event occurs. You can use any valid subroutine as an event handler. You cannot use a function as an event handler, however, because it cannot return a value to the event source.

Visual Basic uses a standard naming convention for event handlers that combines the name of the event sender, an underscore, and the name of the event. For example, the click event of a button named button1 would be named Sub button1_Click. It is recommended that you use this naming convention when defining event handlers for your own events, but it is not required; you can use any valid subroutine name.

Before an event handler becomes usable, you must first associate it with an event by using either the Handles or AddHandler statement.

The WithEvents statement and Handles clause provide a declarative way of specifying event handlers. Events raised by an object declared with WithEvents can be handled by any subroutine with a Handles clause that names this event. Although the Handles clause is the standard way of associating an event with an event handler, it is limited to associating events with event handlers at compile time.

The AddHandler and RemoveHandler statements are more flexible than the Handles clause. They allow you to dynamically connect and disconnect the events with one or more event handlers at run time, and they do not require you to declare object variables using WithEvents.

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



Public Class Printer
 Public PaperJam As EventHandler
 Public OutOfPaper As EventHandler
 Public JobOK As PrinterGoodDelegate
End Class

The following example shows the Printer class, which contains an event handler:



...
Public Function Operation_2() As Object Handles Print
 End Function
...

Working with Properties

Properties are modeled as attributes with a stereotype of <<Property>>, and with one or two linked operations representing the get and/or set accessors.

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.

The Get and Set accessors in Visual Basic 2005 can now have different accessibility settings, as long as Set is more restrictive than Get.

It is possible to add a Property for an existing attribute to access it. The attribute will have the <<PropertyImplementation>> stereotype. The created Property will use the same code as the implemented attribute but starting with an underscore (_) character. By default, the Property will have a public visibility and will not be persistent.

Field, Event, and Property Properties

Visual Basic 2005 field, event, and property property sheets contain all the standard attribute tabs along with the VB, the properties of which are listed below:

Property

Description

Compilation Unit

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

As New

Specifies that the attribute is created by new object instance.

Shadowing

Specifies the form of shadowing. You can choose between:
  • Shadows

  • Overloads

  • Overrides

Default

[properties only] Specifies whether the property is a default.

Overriding

[properties only] Specifies the form of overriding available. You can choose between:
  • Overridable

  • NotOverridable

  • MustOverride

Property Parameters

[properties only] Specifies the parameters of the property.