Hello everyone,
I have a question re variable scope.
The system I’m converting was written in the Harbour/Clipper dialect and that's what I'm using in the X# conversion.
I have over 100 variables declared as ‘PUBLIC’ in the top (controlling) module (let’s call it ‘TOP.PRG’). These are all ‘set’ at that level before any of the dozens of other modules (let’s call them ‘MOD1.PRG’, ‘MOD2.PRG’, ‘MODn.PRG …) are called.
How can I best declare these variables so they are ‘visible’ by the called modules? For example, I have a public var that tracks the user’s preferred language and which is set at the very beginning (TOP.PRG) based on the user’s login. The var is referenced by just about every called module (MODn.PRG).
EDIT: the variables should not only be visible my the called modules, but also modifiable. For example, MOD3.PRG should be able to change the preferred language variable in a global way.
As always, thanks for your help.
Roland
Q re variable scope
Re: Q re variable scope
I just thought of something: s a class using get() set() methods the best option?
Re: Q re variable scope
Hi Roland,
in a pure .NET way you can use a singleton object.
Please look at this code:
And then you can read and write to this variable like this
If you need explanations how this works, please let me know.
Wolfgang
P.S. I hope to no have any typing errors. I use classes like these in all my X# applications
in a pure .NET way you can use a singleton object.
Please look at this code:
Code: Select all
class ValStorage
static protect initonly _oCurrent as ValStorage
protect _cMyVar1 as string
static constructor()
_oCurrent := ValStorage{}
return
protected constructor()
_cMyVar := "Roland" //Initialization
return
static property MyVar1 as string get _oCurrent:_cMyVar1 set _oCurrent:_cMyVar1 := value
end classe
Code: Select all
ValStorage.MyVar1 := "not Roland"
Wolfgang
P.S. I hope to no have any typing errors. I use classes like these in all my X# applications
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Re: Q re variable scope
Roland,
A simple but effective solution is also to use global variables. These become static members of the compiler generated function class.
You can control their visibility like this:
Typing of the GLOBAL is optional, but recommended. You can always type the global as OBJECT or USUAL if the global may hold more than one type.
Robert
A simple but effective solution is also to use global variables. These become static members of the compiler generated function class.
You can control their visibility like this:
Code: Select all
INTERNAL GLOBAL Foo AS STRING // visible everywhere in the current assembly
PUBLIC GLOBAL Bar AS INT // Also visible in code that references the assembly
STATIC GLOBAL FooBar as LOGIC // Only visible in the current module
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
Re: Q re variable scope
Hi Roland,
Another note, just in case you are not aware, PUBLICs and PRIVATEs are also supported in X#, and you can still use them like you did in Harbour/Clipper/VO, you just need to enable the related compiler option for them (/memvar), which you can find in the compiler page of the app properties.
So this option is still there for compatibility (and making it easier to port existing code more easily), but of course it's indeed a lot better if you replace them with strongly typed GLOBALs or a singleton class as the guys already suggested.
Another note, just in case you are not aware, PUBLICs and PRIVATEs are also supported in X#, and you can still use them like you did in Harbour/Clipper/VO, you just need to enable the related compiler option for them (/memvar), which you can find in the compiler page of the app properties.
So this option is still there for compatibility (and making it easier to port existing code more easily), but of course it's indeed a lot better if you replace them with strongly typed GLOBALs or a singleton class as the guys already suggested.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
Re: Q re variable scope
thanks for the quick help folks!
Tested with PUBLIC GLOBAL and all is good.
Cheers
Roland
Tested with PUBLIC GLOBAL and all is good.
Cheers
Roland