/ Dynamics

[X++] setPrefix explanation and use

Basics

setPrefix is a function to create headings in an infolog. It helps to identify the scope tho which the message belongs to and should be used whenever it makes sence.

Syntax

setPrefix(str prefix)

The parameter is a string value representing the text you want to set as heading.
You can get more details here on MSDN.

Important!

Scope

The prefix depends on the scope your code is in. setPrefix adds the prefix for the current execution scope. When leaving the scope the prefix is automatically reset to the previous level. To retrieve the current execution prefix you can use getPrefix().

Root element

The infolog has a root element. If you don't call setPrefix it contains something like Message (10:23:29).
(This comes from classmethod info.newHeadLine.)
The first call to setPrefix will change this root element. So be sure to initial call setPrefix when you want to set more than one prefix in your process.

Example

static void prefixExample(Args _args)
{
    void output1()
    {
        setprefix("Ouput1");
        info("This text is in output1");
        info("Scope is output1");
    }

    void output2()
    {
        setprefix("Ouput2");
        info("This text is in output2");
        info("Scope is output2");
    }
    ;

    // Important to set this!
    // You can only have 1 root prefix and if you don't set it, all calls to setprefix will belong to this root element.
    // Then only the first call will take effect.
    // Test it by commenting out the next line.
    setPrefix("Main Heading");
    output1();
    output2();
}

Result:

Result without the inital call:

[X++] setPrefix explanation and use
Share this

Subscribe to robsCode