Array::Add

Description

Adds one element to the end of the array

Syntax

int Add( _TemplType newElement )

Parameters

newElement
[input] The element whose reference is added to the array

Return

Returns the index at which the element is added, also will be the upper-bound of the array

Examples

EX1

#include <Array.h>
struct PERSON
{
        int nID;
};
void Array_Add_ex1()
{
        Array<PERSON&> arp(true); //set as owner, will destroy all the items in it when get out of this function, though some items may be created in heap
        for ( int ii = 0; ii < 4; ii++ )
        {
                PERSON* tp = new PERSON; //add variable created in heap
                tp->nID = ii + 1;
                arp.Add(*tp);
        }
        
        PERSON& rp = arp.GetAt(0);
        rp.nID = 258; //note, change rp's value will change the first item's value since they refer to the same object
        
        for ( ii = 0; ii < arp.GetSize(); ii++ )
        {
                printf("Person %d's ID is : %d\n", ii+1, arp.GetAt(ii).nID);
        }
}


Ex2 Array of StringArray

#include <Array.h>
// This example copy data from Active book to another book
// StringArray is used to hold values from each column
// fill Book1 with some junk
// then new a Book2
// activate book1 to run
void Array_copy_wks(string strTargetBook = "Book2")
{
        Worksheet wks = Project.ActiveLayer();//assume Book1
        if(!wks)
                return;
        int nCols = wks.GetNumCols();
        WorksheetPage wp2(strTargetBook);
        if(!wp2)
                return;
        
        Worksheet wks2 = wp2.Layers();
        if(!wks2)
                return;

        if(wks2.GetNumCols() < nCols)
                wks2.SetSize(-1, nCols);
        //by default, Array constructor bOwner = false, so better
        //set bOwnder=true so the new StringArray pointers can be automatically deleted
        Array<StringArray&> sSrc(true);
        int ii;
        for(ii = 0; ii < nCols; ii++)
        {
                StringArray* psa = new StringArray;
                wks.Columns(ii).GetStringArray(*psa);
                sSrc.Add(*psa);
        }
        for(ii = 0; ii < nCols; ii++)
        {
                wks2.Columns(ii).PutStringArray(sSrc.GetAt(ii));
        }
}

Remark

See Also

header to Include

Array.h