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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Like operator in Switch Case statements?

(0) ShareShare
ReportReport
Posted on by

Does anyone know if there is a way to use a wildcard in a switch statement?

I know in an if statement this is valid

str string = "whatever comes after whatever";
if (string like "whatever*")
{
info("Works");
}


but is it possible to do with with a switch? i haven't been able to find a good way to do it except for the following work around, which has a caveat.

str string = "whatever comes after whatever";
str startswithstring;
;
startswithstring = substr(string, 8, -8); //but what if you dont know the length of the string?
switch (startswithstring)
{
case "whatever" :
info("works");
break;
}


so its ok if you know the exact length of the string in which you want to search.  but what if the string is of an unknown length?  this workaround would only be good if you knew the length of the original string.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mea_ Profile Picture
    60,284 on at

    Hi itemLouks,
    Any switch could be replaced with multiple "if .. else if", what is the reason to use switch ?

    If you want to do this you can use next code:

    switch (true)
    {
        case (string like "whatever*") :
            info("works");
            break;
    }


  • Community Member Profile Picture
    on at

    I have written a method that uses a stringedit to function as a "smart filter".  So depending on what is typed into the stringedit i can use in a query in different field ranges and values or perform different calculations all from one textbox.  In doing so i have created quite a large nested if statement. While it does work, it is just plain ugly and hard to read.

  • Community Member Profile Picture
    on at

    its funny i tried 100 different ways i never thought to just put (true) after the switch.  It makes sense now i really appreciate the help.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans