Moving Legacy Classes to the SAS Component Object Model |
In SAS/AF software, the information about a class is stored in either instance variables (as is the case with legacy classes) or attributes. However, attributes are not the same as instance variables. A standard instance variable (IV) consists of the name, type, and value of a variable. An attribute contains the name, type, and value, plus additional information in its metadata to provide features such as validation and linking.
Within the SCL list that defines the class, attributes and IVs are stored separately. However, IVs remain as part of the class structure for compatibility reasons only.
Attributes, then, contain more information, offer more functionality, and are easier to query and set through SCL. For example, if you have a legacy class with an instance variable called color, you would not directly modify the underlying IV. Instead, you would define GET and SET methods (such as getColor and setColor) to query and change the value. In new SAS software releases, however, you implement color as an attribute, and you can use SCL dot notation to query and set its value. For example:
objectName.color='red';
Unless additional processing is needed when a user sets or gets the value for color, the class does not need setColor or getColor methods.
Converting Instance Variables to Attributes |
SAS/AF software enables you to convert your instance variables to attributes. When a SAS/AF legacy class is edited or instantiated, an attribute is created for every IV in the class. Attribute names match their IV counterpart names. A link is also created between each attribute and the corresponding IV so that the actual storage of the value remains with the IV.
When you edit a legacy class using the Class Editor, you will see the attributes that have been created for you in the Attributes table. If you scroll to the right of the table, you will also see a column labeled IV LINK. This column can contain one of three values:
Basically, all the functionality for an object that you previously accessed through the Object Attributes and Region Attributes windows can be surfaced in the Properties window as class attributes. Linking IVs to attributes does not break existing behavior for your class, yet it enables you to take advantage of
dot notation to set or retrieve data items that were previously available to you via the Object Attributes window and Region Attributes window
attribute linking, which facilitates rapid application development by enabling you to define and then connect attributes of different components
Accessing Instance Variable Values with Dot Notation |
You can access IV values just as you did in previous versions of SAS/AF software. You can also take advantage of dot notation to access IV values through attributes.
The following example shows how to use dot notation to access the value of an IV that is linked to an attribute:
If _self_.backgroundColor = 'red' then do; /* to query */ _self_.backgroundColor = 'blue'; /* to set */
Without the _self_ qualifier, the compiler would treat backgroundColor like a local SCL variable. Also notice that you do not have to use the GETNITEM or SETNITEM SCL functions. Those functions can be replaced with the above code to improve readability and maintainability.
The following example shows how to use dot notation to access the value stored in an automatic IV that is linked to an attribute:
BackgroundColor = 'red'; /* to set */ If backgroundColor = 'red' then do; /* to query */
This example works because the compiler searches through all the method code to identify variables that have the same name as an IV. If the IV is an automatic IV, it performs a copy in at the beginning of the method and a copy out at the end of the method. Because the value of the automatic IV is not copied out until the end of the method, you can use dot notation if you want to immediately set the IV on the object:
If _self_.backgroundColor = 'red' then do; /* to query */ _self_.backgroundColor = 'blue'; /* to set */
Limitations of Instance Variables |
In general, you cannot use SCL dot notation on instance variables in a class. To create, set, or retrieve values that are stored in an instance variable, you must use the appropriate SCL list functions. Alternatively, you can create a new attribute and use the object's _addAttribute method to link the attribute to any instance variable that you have added. For example, you might want to link your bcolor instance variable to an attribute named backgroundColor:
dcl object id; init: id=loadclass('sasuser.test.MyClass.class'); attr=makelist(); rc=insertc(attr, 'backgroundColor', -1, 'name'); rc=insertc(attr, 'bcolor', -1, 'IV'); id._addAttribute(attr); id._save(); rc=dellist(attr); return;
Copyright © 2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.