$Name from a parameter
Hi IRIS experts!
I have a parameter in a class that stores a global name:
Parameter GlobalName = "^AGlobal";
then I'm trying to call it as:
set gn=$name(..#GlobalName)
to use it later as:
set @gn@("index")="value"
But! I'm having syntax at set gn=$name(..#GlobalName)
What am I doing wrong?
Comments
The syntax issue with your line set gn=$name(..#GlobalName) likely stems from the improper use of # and the .. syntax to reference the Parameter value stored in your class. The $NAME function in ObjectScript returns the canonical name value of a variable or portion of a subscript reference. However, it works correctly only with properly defined references [1].
To resolve your problem:
- Ensure that
GlobalNameis correctly defined in your class as aParameterso it can be referenced. - When calling
$NAME, use proper syntax for object property or parameter references. IfGlobalNameis a class parameter, replace..#GlobalNamewith the evaluated value or other valid syntax that provides the global name string to$NAME.
The $NAME function returns a canonical name of a variable or subscripted reference, allowing you to subsequently use the @ indirection operator on the resulting string [1][2]. Here's a simplified example:
set gn=$name(^AGlobal)
set @gn@("index")="value"
Make sure ^AGlobal corresponds to the actual global structure you intend to modify or reference [1][2]. If rephrased, or if a specific class definition is missing, additional context might be required.
Sources:
Thank you, @DC AI Bot - but your answer is not super relevant to the situation. I really want to store the global name in a class parameter and don't see what is wrong here.
It doesn't look like you are doing anything wrong but there must be something in the compiler that doesn't like you.
$Name hates class parameters but seems OK if you @ it
Parameter GlobalName = "^Glob(1)";ClassMethod TestG() As %String [ ProcedureBlock = 0 ]{ s G=..#GlobalName
// This only compiles when ProcedureBlock=0 q $Name(G)
// This will not compile
; s GN=$Name( ..#GlobalName )
// But this does and it works
Q $Name( @..#GlobalName )
}https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl… reading this part of the docs, $Name specifically asks for a variable, so a class param needs to be bound to a variable to be valid
Exactly.!
..#PARAMETER inserts a STRING
But >>> $NAME() looks for a variable also by indirection
USER>w$name("^rcc(1)")
W$NAME("^rcc(1)")
^
<SYNTAX>
USER>w$name(^rcc(1))
^rcc(1)
USER>sx="^rcc(1)"
USER>w$name(x)
x
USER>w$name(@x)
^rcc(1)
USER>Thank you, very clear now!
Studio shows the Error already during typing..png)
But this works fine as you expected:.png)
BINGO !
.png)
composed by nI
The generated .INT proves it.png)
Hello @Evgeny Shvarov
We can define the global name using either a compile-time class parameter or a runtime expression (COSEXPRESSION):
- Compile-time:
Parameter GlobalName = {$NA(^AGlobal)}; - Runtime (COSEXPRESSION):
Parameter GlobalName As COSEXPRESSION = "$NA(^AGlobal)";
Instead of assigning just the global name to the parameter and then later generating the full reference using $NAME, you can directly assign the full $NA(^AGlobal) expression to the parameter.
This eliminates the need to do something like:set gn = $name(..#GlobalName)
Parameter GlobalName As COSEXPRESSION = "$NA(^AGlobal)";Parameter GlobalName1 = {$NA(^AGlobal)};ClassMethod SetGbl()
{
Set @..#GlobalName1("test")=112zw @..#GlobalName
}
Oh, Wow!
So many great answers! thank you @Stuart Strickland, @Chris Stewart , @Robert Cemper , @Ashok Kumar T
!
It was not obvious with $name and Class Parameter - now it is clear! Thanks a lot!
If you want to work with class parameters, you can choose between two basic variants
Parameter GlobalName1 = "^Global";Parameter GlobalName2 = {$name(^Global)};ClassMethod Test()
{
set @$name(@..#GlobalName1)@("index")="abc"// using variante 1write @..#GlobalName2@("index") // using variante 2
}Thank you, @Julius Kavay