Tweene
Tweene

Reputation: 297

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;
 }

Upvotes: 4

Views: 4382

Answers (2)

FH-Inway
FH-Inway

Reputation: 5107

This is not possible in , as there is no tuple pattern as in .

Edit: As @Jan B. Kjeldsen shows in his answer, containers can be used similar to how tuple patterns can be used in to create a switch statement with multiple inputs.

Upvotes: 1

Jan B. Kjeldsen
Jan B. Kjeldsen

Reputation: 18051

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.

Upvotes: 5

Related Questions