Q re variable scope

This forum is meant for questions and discussions about the X# language and tools
Post Reply
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Q re variable scope

Post by RolWil »

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
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Re: Q re variable scope

Post by RolWil »

I just thought of something: s a class using get() set() methods the best option?
User avatar
wriedmann
Posts: 3765
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Q re variable scope

Post by wriedmann »

Hi Roland,
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
And then you can read and write to this variable like this

Code: Select all

ValStorage.MyVar1 := "not Roland"
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4540
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: Q re variable scope

Post by robert »

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:

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
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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4936
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Q re variable scope

Post by Chris »

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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Re: Q re variable scope

Post by RolWil »

thanks for the quick help folks!

Tested with PUBLIC GLOBAL and all is good.

Cheers
Roland
Post Reply