Written by

Independent Consultant
Question Stella Ticker · Feb 21, 2019

Alternative to using % to make a variable public

I am reviewing some code where the % sign has been used liberally to name arrays that are worked on by different methods within the same class. Apparently it is not good practice to name variables with a "%" sign as the first character because this could overwrite other similarly named variables from other developers, including ISC!  Is there another way to make a variable public ? 

Comments

Alan Thatcher · Feb 21, 2019

To specify the list of public variables for the method, use the following syntax:

Method name(formal_spec) As returnclass [ PublicList = variablelist ]
{
   //implementation
}

Where publiclist is either a single variable name or a comma-separated list of variable names, enclosed in parentheses.

In the documentation search for publiclist and you can get the details

0
Stella Ticker  Feb 21, 2019 to Alan Thatcher

Goodness me, this works! Thank you

I did have to add the PublicList  attribute to both the calling and the invoked methods, otherwise it would not work!

Ex

ClassMethod Main() [PublicList =(var1, var2)]
{
    Do ..InvokedMethod()
    W !,var1
}
ClassMethod InvokedMethod() [PublicList =(var1, var2)]
{
    Set var1 = "ChangedInInvoked
}

0
Eduard Lebedyuk  Feb 22, 2019 to Robert Cemper

Small addition: objects are always passed by reference, so usually you don't need to pass them with dot.

0
Robert Cemper · Feb 21, 2019

for that case passing the variables by reference should be sufficient. 

ClassMethod Main() 
{
    Do ..InvokedMethod(,var1,.var2)
    W !,var1
}
ClassMethod InvokedMethod(byRef var1, byRef var2) 
{
    Set var1 = "ChangedInInvoked"
}
0
David Brock  Feb 23, 2019 to Rod Dorman

Variables should never be global in scope.

As others have said they should be part of class instances or passed by reference.

PPGs are intended more for very local, non persistent, storage - e.g. private data within a spawned job.

0
Rod Dorman  Feb 25, 2019 to David Brock

The question was about alternatives to using % which is what I answered.

0
Rod Dorman · Feb 22, 2019

You could also use process-private globals

0
Herman Slagman · Feb 24, 2019

In a true OO-fashion, you should use private properties for that. Available anywhere within your (instantiated) class, but not for the 'outside' world.

0