Search code examples
axaptax++

X++ switch case with multiple inputs


Is there a way like in C# to create a switch with multiple variables? something like this..

 switch((_dim1,_dim2,_dim3))
 {
        case(1, ""):
            Info("1 is null");
        case (2, ""):
            Info("2 is null");
        case (3, ""):
        break;
 }

Solution

  • Switch with multiple arguments have been possible since version 1.0 (+23 years). It requires the use of containers. There is no dont-care value.

    The following job demonstrates:

    static void TestConSwitch(Args _args)
    {
        int dim1 = 2;
        str dim2 = '';    
        switch ([dim1, dim2])
        {
            case [1, '']:
                info("Case 1");
                break;        
            case [2, '']:
                info("Case 2");
                break;
            default:
                info("Default!");
        }
    }
    

    In this case displays "Case 2".

    That said, performance of this may not be great, as it requires the build of containers for both the target value and all the case values (if no match). Looks kind of neat though.