Collection Classes



Struct Sample


static void Collection_Struct()
{
    // Create a struct with two fields
    struct myCar = new struct ("int ModelYear; str Carbrand");
    int i;
    ;
    // Set values to the fields
    myCar.value("ModelYear", 2000);
    myCar.value("Carbrand", "BMW");
    // Add a new field and give it a value
    myCar.add("Model", "316");
    // Loop through the fields of the struct
    for (i=1; i<=myCar.fields(); i++)
    {
        info(strfmt("FieldType: %1, FieldName: %2, Value: %3",
                    myCar.fieldType(i),
                    myCar.fieldName(i),
                    myCar.value(myCar.fieldName(i))));
    }
}


Set Sample


static void Collection_Set()
{
    // Create a new set of type String
    Set cars = new Set(Types::String);
    SetEnumerator   setE;
    ;
    // Add elements to the set
    cars.add("Toyota");
    cars.add("Ford");
    cars.add("Mazda");
    // Check to see if an element
    // exist in the set
    if (cars.in("Toyota"))
        info ("Toyota is part of the set");
    // Display the content of the set
    info (cars.toString());
    // Get the enumerator of the set
    // to loop through it
    setE = cars.getEnumerator();
    while (setE.moveNext())
    {
        info(setE.current());
    }
}

List Sample


static void Collection_List()
{
    // Create a new list of type string
    List names = new List(Types::String);
    ListEnumerator  listE;
    ;
    // Add elements to the list
    names.addEnd("Lucas");
    names.addEnd("Jennifer");
    names.addStart("Peter");
    // Display the content of the list
    info (names.toString());
    // Get the enumerator of the list
    // to loop through it
    listE = names.getEnumerator();
    while (listE.moveNext())
    {
        info (strfmt("Name: %1", listE.current()));
    }
}

Map Sample


static void Collection_Map()
{
    // Create a new map with a key and value type
    Map cars = new Map(Types::Integer, Types::String);
    MapEnumerator    mapE;
    ;
    // Insert values to the map
    cars.insert (1, "Volvo");
    cars.insert (2, "BMW");
    cars.insert (3, "Chrysler");
    // Display the content of the map
    info (cars.toString());
    // Get the enumerator to loop
    // through the elements of the map



    mapE = cars.getEnumerator();

    while (mapE.moveNext())
    {
        info(strfmt("Car %1: %2", mapE.currentKey(),
          mapE.currentValue()));
    }
}


No comments:

Post a Comment