Compartir a través de


Struct.add(String, Object) Method

Definition

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

public:
 virtual bool add(System::String ^ _fieldName, System::Object ^ _value);
public virtual bool add (string _fieldName, object _value);
abstract member add : string * obj -> bool
override this.add : string * obj -> bool
Public Overridable Function add (_fieldName As String, _value As Object) As Boolean

Parameters

_fieldName
String

The value to assign to the new item. This value defines the type of the item.

_value
Object

The value to assign to the new item. This value defines the type of the item.

Returns

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.

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; 
}

Applies to