4. Working with Classes : How You Can Manipulate Objects Generically: Casting : How You Can Work with Attributes
 
Share this page                  
How You Can Work with Attributes
Because the TriggerField attribute can be used on any field or menu item, you must make explicit to OpenROAD the class of the object being manipulated in order for your code to manipulate specific attributes and methods. Casting involves making the system class of a generic object explicit.
To cast a generic object, enclose its reference variable in parentheses and precede the parentheses with the name of the subclass to which you want to cast it. If, for example, you want to change the background pattern of a field as the user enters it, you use the BgPattern attribute defined for FormField. The following example uses casting to specify that the generic TriggerField is an entry field and changes the background pattern of the field to shaded.
EntryField(CurFrame.TriggerField).BgPattern =
    FP_SHADE;
You can cast a generic object to a more specific object only if the specific object is a subclass of the generic one. This casting example is possible because the data type of FrameExec's TriggerField attribute is FieldObject, and FormField and EntryField are both subclasses of FieldObject.
Although the class to which you cast a generic object must be in the hierarchy below the generic object, you can specify any valid class as the type for the generic object. For example, to change the background pattern of a field, you can cast the generic object to FormField or any field below FormField on the hierarchy. The following example works similarly to the previous example, but allows all form fields to trigger the change in background pattern:
FormField(CurFrame.TriggerField).BgPattern =
    FP_SHADE;
When you cast an object, OpenROAD produces an error at runtime if the actual object is not of the same class as, or a subclass of, the class to which you have cast it.