Global variables for multi-windows app

Dear all, I’m a newbie and I have a problem.
I had an app with one single screen, having several Properties (as Public) used to store data exchanged between different actions and functions. Everything worked well.

Now I need to modify the system to move to multi windows… a main windows and buttons to open/close sub-windows to run specific tasks or show more data.
My issue is Properties as Public in Main window are not seen in sub-windows and therefore I cannot use the stored value.
I read about Global scope, but I cannot find it in Property-Inspector-scope (I see only Private, Public and Protected…) .

Normally I load a value in Event-Open and store it in my “global-variable” and use the value where needed around…
example: SERVERIP is the string-global-variable used to store serverIP and I need to use around my windows…

How to declare and initialize in Main-window in order to be accessible and readable in all sub-windows??
Where’s my mistake? What should I do differently to get this results

Thank you so much for any support on it.
Marco

store your properties as global in a new module. or possible too as global property of “App”

Oh gosh please stay away from Global variables for this. You’ll write yourself into a corner that will be very hard to get out of in the future. It’s very tempting for a first project, and is mostly the reason Answers 2 never came to be.

I share a complete document-based desktop application on my github that I think might be very helpful for you. This is how I build my document based apps, and VersionTracker is a fork from this project: https://github.com/devtimi/Retinassets

It sounds like your issue is that the children windows aren’t aware of the parent. You can solve this by passing the parent reference to the children when you create / open them; however in most cases this shouldn’t be necessary at all. Can I ask you why you need this reference? Chances are you don’t, and I’d be happy to help you figure this one out :slight_smile:

Edit: forgot the word “figure”

Exactly like Jean-Yves says. For myself, I use the global propery “App.” … Let’s say I have a database MyDb which is used in all the program and in different Windows. I will refer it in my code as app.MyDb and it will be available everywhere :slight_smile:

Hope it helps :slight_smile:

Thank you all for replying me. I’m going to try as you suggested.

Concerning Tim’s question, I’m using Xojo to build an interface with industrial instruments and PLC.
I need to load from text files or databases some specific strings or integer/double values and pass it to some points just to recalculate or to concatenate or display or passing text commands to instruments…
Load in a variable that can be read everywhere and used when needed… that’s the basic… I’m more electronic than software engineer, try my best but …

I’ll look for your documentation to learn… Thank you so much!

Doesn’t this lead to very tight coupling?
For a newbie, and especially if the number of global variables is low, app properties are an easily understood and reliable way to pass data about.
In this mindset, a window is as much an attribute of the App as a variable is.

I wonder: Why should a child window have an intimate knowledge of its parent in this way?
A more pure approach would be to create a constructor on the window that takes ‘info which it needs’ as a parameter.
if it then ever needs to be used anywhere else, the ‘parent’ can change seamlessly … any parent could create a child and pass the right values into the constructor, as opposed to having the child hope that the parent is of the type it has come to expect.

All that said, I’ll have a look at the example project at some point, as I’ll be interested to see how Tim resolves this. :slight_smile:

I would very much agree that passing window references around is a more advanced topic.

As I’ve said in my post, “however in most cases this shouldn’t be necessary at all.
The desire to access something on another window is usually because the developer wants to do something different that doesn’t need the window at all. For example, as Marco has described their use case, instead of needing to know about the other windows, the children should have access to the database, text file, or string/double values directly. That’s why I’d asked, my approach is to try and find out what someone wants to do, and help them get the result they actually want.

This frequently comes out as: “describe what you want the user to do, not the technology you think you need.”

You and I both agree here, but we got there by different means :slight_smile:

I think we agree on most of this, Jeff, and are just communicating differently. I would simply add that in my opinion Globals do not deserve the title “reliable”. I would be happy to concede to “easy to use” for sure :wink:

completely agree with Tim.
you can store your globals on a module like I said, but use as few globals as possible.
take profit from a good oop implementation and store each property with its related object.