Question
Mack Altman · Jun 25, 2018

Help with dynamic Xecute in curly brackets

Typically, I have time to review the documentation, which I'm sure is here. However, I have a workaround (TEST1) but I was wondering if anyone could assist me in advising how I would need to adapt the curly brace snippet (TEST2) to provide the same result.

Thanks for any help you can provide.

TEST1(STATUS=1) K (STATUS)
 X "S MSG=$S(STATUS:""HELLO"", 1:""GOODBYE"")"
 
 W !,MSG
 Q
 
TEST2(STATUS=1) PUBLIC {
X "S MSG=$S(STATUS:""HELLO"", 1:""GOODBYE"")"

W !,MSG
}
0
0 241
Discussion (3)2
Log in or sign up to continue

2 possible solutions:

TEST3(STATUS=1) [STATUS,MSG] PUBLIC {
 "S MSG=$S(STATUS:""HELLO"", 1:""GOODBYE"")"
 !,MSG
}
TEST4(%STATUS=1) PUBLIC {
 "S %MSG=$S(%STATUS:""HELLO"", 1:""GOODBYE"")"
 !,%MSG 
}

see also my article Summary on Local Variable Scoping

Have you considered using $xecute?

Version:1.0 StartHTML:00000092 EndHTML:00001371 StartFragment:00000172 EndFragment:00001337 CachÙ Studio clip  Class User.Exec [ Abstract ]
{

ClassMethod test(STATUS = 1) As %String
{
set greeting = $xecute("(STATUS) { return $S(STATUS:""HELLO"", 1:""GOODBYE"") }",STATUS)
return greeting
}

}

and here is this code in action:

PANTHER:XEP:DEV>w ##class(Exec).test()

HELLO

PANTHER:XEP:DEV>w ##class(Exec).test(0)

GOODBYE
TEST1(STATUS=1)
 MSG
 ("(IN,OUT) S OUT=$S(IN:""HELLO"", 1:""GOODBYE"")",STATUS,.MSG)
 
 !,MSG
 Q
 
TEST2(STATUS=1) PUBLIC {
 ("(IN,OUT) S OUT=$S(IN:""HELLO"", 1:""GOODBYE"")",STATUS,.MSG)

 !,MSG
}