Setting Properties, Methods, and Events
Go Up to What Goes into a Component
Aside from the visible image manipulated in the Form designer, the most obvious attributes of a component are its properties, events, and methods. Each of these has a section devoted to it in this file, but the discussion that follows explains some of the motivation for their use.
Properties give the application developer the illusion of setting or reading the value of a variable, while allowing the component writer to hide the underlying data structure or to implement special processing when the value is accessed.
There are several advantages to using properties:
- Properties are available at design time. The application developer can set or change initial values of properties without having to write code.
- Properties can check values or formats as the application developer assigns them. Validating input at design time prevents errors.
- The component can construct appropriate values on demand. Perhaps the most common type of error programmers make is to reference a variable that has not been initialized. By representing data with a property, you can ensure that a value is always available on demand.
- Properties allow you to hide data under a simple, consistent interface. You can alter the way information is structured in a property without making the change visible to application developers.
The section Creating Properties - Overview explains how to add properties to your components.
Class methods are procedures and functions that operate on a class rather than on specific instances of the class. For example, every component's constructor method (Create) is a class method. Component methods are procedures and functions that operate on the component instances themselves. Application developers use methods to direct a component to perform a specific action or return a value not contained by any property.
Because they require execution of code, methods can be called only at run time. Methods are useful for several reasons:
- Methods encapsulate the functionality of a component in the same object where the data resides.
- Methods can hide complicated procedures under a simple, consistent interface. An application developer can call a component's AlignControls method without knowing how the method works or how it differs from the AlignControls method in another component.
- Methods allow updating of several properties with a single call.
The section Creating Methods - Overview explains how to add methods to your components.
An event is a special property that invokes code in response to input or other activity at run time. Events give the application developer a way to attach specific blocks of code to specific runtime occurrences, such as mouse actions and keystrokes. The code that executes when an event occurs is called an event handler.
Events allow application developers to specify responses to different kinds of input without defining new components.
The section Creating Events - Overview explains how to implement standard events and how to define new ones.