Monday, November 9, 2015

The better way to pass containers between objects/forms using the Args() class, and not by converting to a string

If you need to pass a container between objects/forms using the Args class, don't convert it to a string and then back, use parmObject() and ContainerClass()!  I see many suggestions about converting it to a string, which can have much more unpredictable results and is not as versatile.

You wrap your container in the class ContainerClass() and then unwrap it at the other end.

Make your call like this:
args.parmObject(new ContainerClass(["Real container", 1234, "Not con2str container"]));
And retrieve it like this:
containerClass = element.args().parmObject() as ContainerClass;
myContainer = containerClass.value();

To test this, create a form (Form1) and overwrite the init method and put in this code:

public void init()
    ContainerClass      containerClass;
    container           conValue;
    if (!(element.args() && element.args().parmObject() && element.args().parmObject() is ContainerClass))
        throw error("@SYS22539");
    containerClass = element.args().parmObject() as ContainerClass;
    conValue = containerClass.value();
    info(strFmt("The container contains '%1'", con2Str(conValue)));

Then create a Job and put in this code:

static void JobForm1(Args _args)
    Args        args;
    FormRun     formRun;
    args = new Args();;
    args.parmObject(new ContainerClass(['Real containers', 1234, 'Not con2str containers']));
    formRun = classFactory.formRunClass(args);

And then run the job!

No comments:

Post a Comment