web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Conditional / dynamic strFmt call

(0) ShareShare
ReportReport
Posted on by

Hello:

Is it possible to write conditional logic to build a call to strFmt. What I'd like to do read in the string parameter with the placeholders, then depending on certain conditions, dynamically add the values for the placeholders. In one call, I would send three strings, while in another call I may only need to send two. I would build the call in a while or for loop. 

Hopefully that made some sense. 

Thank you.

*This post is locked for comments

I have the same question (0)
  • Hajish Profile Picture
    460 on at
    RE: Conditional / dynamic strFmt call

    can you please share the screens what you expecting to do?

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Conditional / dynamic strFmt call

    I don't see why you want to do this but its possible.  Just loop on the string while reading in each character.

    Pseudo:

    Find strlen() of string, use results to loop on said string using substr to get each char if necessary.  From there add your conditions.

  • Community Member Profile Picture
    on at
    RE: Conditional / dynamic strFmt call

    Hi Hajish,

    There's no screen I can share but here's a bit of code I'm trying to accommodate.

       comp = new XppCompiler();

       expression = strfmt("(%1/(%2-%1))*123",num2str(a,0,4,1,0),num2str(b,0,4,1,0));

       if (comp.compileExpr(expression))

           info(strFmt("%1",comp.execute(expression)));

    I would like to be able to dynamically pass as many variables as I need to the strfmt call based on some other parameter. In the code above, my first parameter would be a formula I would store, and based on the formula it would dictate how many values I need to pass to it in the call.

    Hope that helped.

  • Suggested answer
    Mea_ Profile Picture
    60,284 on at
    RE: Conditional / dynamic strFmt call

    I know only one solution for this, it's ugly but used across AX widely.

    You need to create a method that accepts container with your parameters, something like this :

    public str myMethod(str _string, container _parmsCon)
    {
        switch (conLen(_parmsCon))
        {
            case 1:
               return strFmt(_string, conPeek(_parmsCon, 1));
            case 2:
               return strFmt(_string, conPeek(_parmsCon, 1), conPeek(_parmsCon, 2));
            case 3:
               return strFmt(_string, conPeek(_parmsCon, 1), conPeek(_parmsCon, 2), conPeek(_parmsCon, 3));
                   
            //and so on
                    
        }
    }

    You can even create a script to generate this code for n parameters :) 

    Now you can use it like:

    myMethod("%1 %2", ['a', 'b']);
    myMethod("%1 %2 %3 %4 %5", [1, 2, 3, 4, 5]);



  • Suggested answer
    PA-22040759-0 Profile Picture
    6,194 on at
    RE: Conditional / dynamic strFmt call

    I have tried to write a method to solve this. The method gets the string to fill the arguments into and a container with the argument values. 

    Here's my class:

    class aaaStrFmt
    {
    }

    private str strFmt(
        str         _expression,
        container   _args)
    {
        str             ret             = _expression;
        List            argsList        = con2List(_args);
        ListEnumerator  argsEnumerator  = argsList.getEnumerator();
        Counter         counter;
    
        while (argsEnumerator.moveNext())
        {
            counter++;
    
            ret = strReplace(ret, '%' + strFmt('%1', counter), argsEnumerator.current());
        }
    
        return ret;
    }

    public static void main(Args _args)
    {
        aaaStrFmt   strFmtTest = new aaaStrFmt();
        
        info(strFmtTest.strFmt("%1 + %2", [1, 2]));
        info(strFmtTest.strFmt("(%1 + %2) * %3 - %4", [5, 8, 10, 25]));
    }
  • Mea_ Profile Picture
    60,284 on at
    RE: Conditional / dynamic strFmt call

    Try to run it using 

    info(strFmtTest.strFmt("(%1 + %2) * %3 - %4", [5, 8.5, 10, 25]));

    and you will get stack trace :) 

  • Suggested answer
    PA-22040759-0 Profile Picture
    6,194 on at
    RE: Conditional / dynamic strFmt call

    Argh. Seems to be a bug in Global::Con2List()

    Traversing the container directly instead will work:

    private str strFmt(
        str         _expression,
        container   _args)
    {
        str             ret = _expression;
        Counter         counter;
    
        for (counter = 1; counter <= conLen(_args); counter++)
        {
            ret = strReplace(ret, '%' + strFmt('%1', counter), strFmt('%1', conPeek(_args, counter)));
        }
    
        return ret;
    }

    I just don't like traversing containers :-)

    You can discuss if you want the last strFmt. The one doing strFmt on the particular value from a position on the container. Doing so will change the decimal to your current locale settings. Where I live, in Denmark, we use comma for decimal separator, so I get "8,5" from an input of "8.5". You might not want that behavior in this case where you pass the string on to XppCompiler.

  • Mea_ Profile Picture
    60,284 on at
    RE: Conditional / dynamic strFmt call

    Not yet :) 

    Try this 

    info(aaaStrFmt.strFmt("%1 + %2", ['%2', '%1']));


    and than compare with standard one :

    info(strFmt("%1 + %2", '%2', '%1'));

    Talking about my previous example you would get stack trace not only with 8.5 but with 'a' as well, so it is not about comma at all. I don't think that it's a bug of con2list it is the way how it was done. 

  • PA-22040759-0 Profile Picture
    6,194 on at
    RE: Conditional / dynamic strFmt call

    The intention was not that you call this with another set of %n values. I'm not trying to build a new StrFmt, just trying to help out our friend who posted the question.

  • Mea_ Profile Picture
    60,284 on at
    RE: Conditional / dynamic strFmt call

    Original questions was quite abstract and as you can see solution based on string replace will fail if parameter would be string with '%' character inside. We don't know what is the real intention and maybe building another strFmt is what he really needs.  Solution proposed by me does not look so short and nice but it does not have this issues as well. So what do you prefer nice but limited solution or big and fully functional ?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#3
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans