Bilal Saeed
Bilal Saeed

Reputation: 603

Collection of objects

I want to store list of objects in X++. I've read in msdn that arrays and containers cannot store Objects, so the only option is to make a list of Collection. I have written the following code and tried to use Collection = new List(Types::AnyType); and Collection = new List(Types::Classes); but both are not working. Please see if I'm making some mistake in the following job.

static void TestList(Args _args)
{
    List Collection;
    ListIterator iter;
    anytype iVar, sVar, oVar;

    PlmSizeRange PlmSizeRange;
    ;
    Collection = new List(Types::AnyType);

    iVar = 1;
    sVar = "abc";
    oVar = PlmSizeRange;
    Collection.addEnd(iVar);
    Collection.addEnd(sVar);
    Collection.addEnd(oVar);    

    iter = new ListIterator(Collection);
    while (iter.more())
    {
        info(any2str(iter.value()));
        iter.next();
    }
}

Moreover, can't we cast some variable or object into Anytype variable, I read out that typecasting is done automatically this way;

anytype iVar;
iVar = 1;

But on running it throws an error that expected type was Anytype, but encountered type is int.

Upvotes: 4

Views: 8939

Answers (1)

Jan B. Kjeldsen
Jan B. Kjeldsen

Reputation: 18061

Last things first, anytype variables takes the type first assigned to it, you cannot change it later:

static void Job2(Args _args) 
{
    anytype iVar;
    iVar = 1;             //Works, iVar is now an int!
    iVar = "abc";         //Does not work, as iVar is now bound to int, assigns 0
    info(iVar); 
}

Back to your first question, new List(Types::AnyType) will never work as the addEnd method tests the type of its argument at runtime, and anytype variables will have the type of the value assigned to it.

Also new List(Types::Object) will only store objects, not simple data types as int and str. It may be contrary to your what you (and C#) believe, but simple types are not objects.

What is left? Containers:

static void TestList(Args _args)
{
    List collection = new List(Types::Container);
    ListIterator iter;
    int iVar;
    str sVar;
    Object oVar;
    container c;
    ;
    iVar = 1;
    sVar = "abc";
    oVar = new Object();
    collection.addEnd([iVar]);
    collection.addEnd([sVar]);
    collection.addEnd([oVar.toString()]);
    iter = new ListIterator(collection);
    while (iter.more())
    {
        c = iter.value();
        info(conPeek(c,1));
        iter.next();
    }
}

Objects do not automatically convert to containers, usually you supply pack and unpack methods (implementing the interface SysPackable). In the above code toString is used which is cheating.

On the other hand, I do not see a use case for your request, that Lists should contain any type. It is against its designed purpose, a List contain one and only one type as defined when the List object is created.

Besides lists there are other collections types, maybe a Struct will suit your needs.

Upvotes: 6

Related Questions