Not quite shared properties

I recently modified a project that was restricted to having a single document window open at a time to allowing multiple simultaneous document windows. In the process, I discovered a problem with a custom class that has some shared properties. I had originally envisioned these shared properties as being global across an application, but now realize that they need to be specific to each document window. And so, I need a different set of these “shared” properties for each document window that is open.

The simplest way I can think of to handle this situation is to move the shared properties into a new class where they will be regular non-shared properties, and have one instance of this new class per window. Then, add a property to the original class that is a reference to this new “shared property” class. Does this seem like a good way to do it? I’m not crazy about doing it this way, because all of these properties are very specific to the class, and it seems to me that they should be a part of the class rather than an external object, but I can’t think of any way to do this within the main class.

Why not simply make them regular properties where they are? Why a new class?

  • Karen

Hi Karen,
The problem is that these shared properties must be accessible to all instances (in the same window) of the class. If a method is called in one instance of the class to change the value of one of these “shared” properties, then this value must be immediately accessible to all other instances of the class (in that window).

Then the properties belong on the window. How the class interacts with the properties will depend on how the class instances are created, etc.

Not sure i understand what you are doing…

Do all instances of that particular window need to share those properties? If so then just putting them as shared on the window would work fine…

If multiple instances of some other class or or instances of multiple cases inside a SINGLE Window instance needs to access them then regular properties on the window would be fine

If You have global methods elsewhere that need to access them, then just have them take the Window instance as a parameter to get at them.

That said, those methods probably really belong on the window too.

  • karen

To answer both of the last two replies: Yes the scope of these properties is the window. I could just create an object in each window with the “shared” properties which the class instances could then access. However, this is a class that I expect to reuse in a number of different projects, and, ideally, in future, I’d like to be able to paste the class into a window in some other project without having to add a bunch of other extraneous properties. I don’t know if this is possible, but I’d rather add some extra code to the class, if possible, to keep it all self contained.

The scope determines where a shared property resides.

If it is shared between all instances of the class in the app then add them globally to the app or a module or the class itself.

If it is shared between all instances of the class on a window then add them to the window.

If it is shared between all instances of the class on a ContainerControl then add them to the ContainerControl.

Make a custom window class with the properties and copy both the class and custom window class to a new project.

If you want to “contain” it then add a folder and copy the folder.

BTW You do know you can subclass the Window Class itself and put those properties (and any code that does not depend on controls existing) there and paste that into any project…

  • karen

Yes. I’d considered subclassing the window. That would work in the current situation, but may not be the best option in future projects where the scope of the shared properties may not be the window. I think I’ll probably create a separate class to hold the shared properties, and create one instance of that in each window. That will work for the present project and will give some flexibility for future projects. In any event, it’s now clear to me that this can’t be handled entirely within the original class.