Share via


Struct.add Method

Adds a new item to the struct and assigns the specified value to it.

Syntax

public boolean add(str fieldName, anytype value)

Run On

Called

Parameters

  • fieldName
    Type: str
    The name of the new item.
  • value
    Type: anytype
    The value to assign to the new item. This value defines the type of the item.

Return Value

Type: boolean
true if the value has been added to the struct; false if the value could not be added (if it already existed in the struct).

Remarks

The type of the value is inferred from content of the value. The items in a struct are arranged in alphabetical order according to the item names. The value of an item in a struct can be changed by using the Struct.value or the Struct.valueIndex method.

Examples

The following example creates a struct with two items name and age fields and assigns values to those items. The add method is then used to create an additional item the shoeSize variable, with a value of 43. The type of the value determines the type of the new item, int.

{ 
    Struct s = new Struct ("str name; int age"); 
  
    // Prints number of items (2) 
    print s.fields(); 
 
    // Values are assigned to the two items 
    s.value("name", "John"); 
    s.value("age", 34); 
  
    //Another item is added to the struct 
    s.add("shoeSize", 43); 
    // Prints number of item (3) 
    print s.fields(); 
    // Prints a description of the items in the struct 
    print s.definitionString(); 
    pause; 
}

See Also

Struct Class

Struct.remove Method

Struct.value Method

Struct.valueIndex Method