Skip to main content

$Case Function

The $Case function evaluates an expression (its first argument), and returns the result that corresponds to the value of the expression (the rest of the arguments). The last argument can be a default result, returned if no other argument value matches the expression.

VS Code - ObjectScript


/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{

ClassMethod Celebrate()
{
    write !, "Yippee! I won!"
}

ClassMethod Complain()
{
    write !, "Oh well, I lost."
}
}

$Case can return literals, as shown in the first example, or it can return names of class methods, as in the second example.

Testing using the Terminal


USER>set survivor = 3

USER>write $case(survivor, 1:"Rich", 2:"Kelly", 3:"Rudy", 4:"Sue", :"")
Rudy
USER>do $case(survivor, 1:##class(ObjectScript.Examples).Celebrate() , :##class(ObjectScript.Examples).Complain() )
Oh well, I lost.
USER>set survivor = 1

USER>do $case(survivor, 1:##class(ObjectScript.Examples).Celebrate() , :##class(ObjectScript.Examples).Complain() )
Yippee! I won!
USER>
FeedbackOpens in a new tab