##
Setting Attribute Values

Some attribute values are read-only and cannot be altered after an ObjectObject has been created. The NinNin attribute of a ZoomMapZoomMap (describing the number of coordinates) is like this. It is defined when the ZoomMap is created, but cannot then be altered.

Other attributes, however, can be modified whenever you want. A ZoomMap's ZoomZoom attribute is like this. If we wanted to change it, this could be done simply as follows:

41#41

which sets the value to 99.6. As when getting an attribute value (§4.5), you have a choice of which data type you will use to supply the new value. For instance, you could use an integer value, as in:

42#42

and the necessary data conversion would occur. You specify the data type you want to supply simply by replacing the final character of the astSetX function name with C (character string), D (double), F (float), I (int) or L (long). Setting a boolean attribute to any non-zero integer causes it to take the value 1.

An alternative way of setting attribute values for Objects is to use the astSetastSet function (i.e. with no final character specifying a data type). In this case, you supply the attribute values in a character string. The big advantage of this method is that you can assign values to several attributes at once, separating them with commas. This also reads more naturally in programs. For example:

43#43

would set values for both the Zoom attribute and the ReportReport attribute (about which more shortly—§4.8). You don't really have to worry about data types with this method, as any character representation will do. Note, when using astSet, a literal comma may be included in an attribute value by enclosed the value in quotation marks:
44#44

Another attractive feature of astSet is that you can build the character string which contains the attribute settings in the same way as when using the C run time library “printf” function. This is most useful when the values you want to set are held in other variables. For example:

45#45

would replace the “%” conversion specifications by the values supplied as additional arguments. Any number of additional arguments may be supplied and the formatting rules are exactly the same as for the C “printf” family of functions. This is a very flexible technique, but does contain one pitfall:

> Pitfall. The default precision used by “printf” (and astSet) for floating point values is only 6 decimal digits, corresponding approximately to float on most machines, whereas the AST library stores such values internally as doubles. You should be careful to specify a larger precision (such as DBL_DIG, as defined in 1#1float.h2#2) when necessary. For example:

> 46#46

Substituted strings may contain commas and this is a useful way of assigning such strings as attribute values without the comma being interpreted as an assignment separator, for example:

47#47

This is equivalent to using astSetC and one of these two methods should always be used when assigning string attribute values which might potentially contain a comma (e.g. strings obtained from an external source). However, you should not attempt to use astSet to substitute strings that contain newline characters, since these are used internally as separators between adjacent attribute assignments.

Finally, a very convenient way of setting attribute values is to do so at the same time as you create an Object. Every Object constructor function has a final character string argument which allows you to do this. Although you can simply supply an empty string, it is an ideal opportunity to initialise the Object to have just the attributes you want. For example, we might have created our original ZoomMap with:

48#48

and it would then start life with its Report attribute set to 1. The “printf”-style substitution described above may also be used here.