The MAKESTRING and STRING Methods

The MAKESTRING method is optional and if present in a class allows for rendering each of its instances as string objects. Each such rendering will usually encode its attributes (object variables) as strings in one form or another.

Whenever an object is supplied as an argument to an instruction or built-in function that works on strings only, a required string value gets requested. If a MAKESTRING method is available it will be run and the resulting string will be used instead.

In the case that there is no MAKESTRING method, then the STRING gets employed instead, which is guaranteed to exist as it gets defined in the Rexx root class Object. The returned string value in this case will usually consist of the indefinite article for the class name and gets concatenated with a blank and with the class name itself.

The exact procedure for getting at a required string value is documented in the Open Object Rexx: Reference , section Required String Values

You can take advantage of this automatic use of MAKESTRING and STRING by overriding (implementing) either or both methods in your own Rexx classes.

The following programs demonstrate STRING and INIT. In the first program, the Part class is created, and along with it, the two methods under discussion, STRING and INIT:


STRING and INIT Methods
/* partdef&per;cmd - Class and method definition file */
/* Define the Part class as a public class */
::class Part public
/* Define the INIT method to initialize object variables */
::method init
expose name description number
use arg name, description, number
/* Define the STRING method to return a string with the part name */
::method string
expose name
return "Part name:" name

In the ::CLASS directive, the keyword PUBLIC indicates that the class can be shared with other programs. The two ::METHOD directives define INIT and STRING. Whenever Rexx creates a new instance of a class, it calls the INIT method of the new instance. The sample INIT method uses an EXPOSE instruction to allow the method direct access to the object variables (attributes) name, description, and number:


Instances in the Part Class

The INIT method expects to be passed three arguments. The USE ARG instruction assigns these three arguments to the exposed object variables (attributes) name, description, and number, respectively.

The STRING method returns the string Part name:, followed by the name of a part. The STRING method (of the Part class) does not expect any arguments. It uses the EXPOSE instruction to be able to directly access the object variable (attribute) name. The RETURN instruction returns the result string.

The following example shows how to use the Part class:


How to use the Part class
/* usepart&per;cmd - use the Part class */
mypartA=.part~new("Widget","A small widge",12345)
mypartB=.part~new("Framistat","Device to control frams",899)
say mypartA
say mypartB
exit
::requires partdef
Results:
Part name: Widget
Part name: Framistat

The usepart&per;cmd program creates two parts, which are instances of the Part class. It then displays the names of the two parts.

Rexx processes all directives before running your program. The ::REQUIRES directive indicates that the program needs access to public class and public routine definitions that are in another program. In this case, the ::REQUIRES directive refers to the partdef&per;cmd program, which contains the Part definition.

The assignment instructions for mypartA and mypartB create two objects that are instances of the Part class. The objects are created by sending a NEW message to the Part class. The NEW message causes the NEW method of the Part class to be invoked, which sends the INIT message together with the three received arguments to the newly created instance, which in turn runs the INIT method as part of the object creation. The INIT method takes the three arguments you provide and saves them in its object's variables (attributes): name, description, and number:

The SAY instruction causes Rexx to create the required string value: as there is no MAKESTRING method in the Part class the STRING method gets invoked to return a string value. The STRING method uses EXPOSE to become able to directly access the name object variable (attribute) and returns it as part of the string (return "Part name:" name).