Accessing Powerbuilder Window Instance Variables from General Function

1.9k Views Asked by At

Blockquote

I have multiple windows that all require the same code, w_emp1, w_emp2, w_emp3…. etc. Thus, it seemed prudent to place this common code into a function say wf_access_test.

Each of these windows has its own set of instance variables. Say is_text_name, is_text_town, is_text_zip_code… etc.

The code for wf_access_test() is below. Note that a specific window (w_emp1) has been hardcoded. This allows me to update that specific windows instance variable.

What I need is a method to address the calling window names instance variables, but replace the hardcoded w_emp1 qualifier

w_emp1.is_text_name = ‘Text Updated’     //Updating this windows instance variable - This works!

I have tried calling the function with the window name as a value passed to the function with a argument type = window.

ie. wf_access_test(w_emp_1)

Unfortunately, when I then try and replace the passed value with the hardcoded w_emp1 it states - incompatible property type?

Any experts out there who can help? Many thanks in anticipation.

2

There are 2 best solutions below

5
On

I would first put the common variables into a structure, so that they are easier to maintain in one place.

You can then add a function on your windows that supports an argument with the type of that structure to update the values inside that window, or just save the structure to your instance and use it directly.

To call the argument, you should be able to use any common ancestor for the windows you are using to hold a reference to the instance; however, when you are calling the function, you will need to use a dynamic call as the base class will not have that method. For example, if you go to w_master as your common ancestor and set a function with argument type w_master, name aw_ref, you could use the call aw_ref.DYNAMIC setData(astr_data) to call the function setData in the child class.

If you check the PowerBuilder help on dynamic calls, it will give you information about considerations when using dynamic calls.

Obviously, the preferred method would be to have your own inherited object to store this, but if you don't want to change the inheritance tree then dynamic calls might be the way to go.

By the way, if you create a new type inherited from one of the common ancestors of your windows, you can change the base class for your windows by editing the source and changing the class it inherits from to your new class; there are only one or two places you need to make the change. I don't know if all the windows you are talking about have the same inheritance tree though, so it might not be worth it if the intermediary child classes have functionality you want.

2
On
  1. Create a first window w_emp.
  2. Define there all what is common between your various windows w_empxx (in particular the instance variables that you need).
  3. In particular, define the function wf_access_test
  4. Create all other windows w_empxx as inherited from w_emp
  5. Specialise your newer windows

Done!

This way, every time you call wf_access_test, Powerbuilder will take it from the ancestor and use the instance variables from the specific window from which you make the call. Of course, this may require some (or a lot of) work but, in the long run, it is way better. Additional benefit: if you need to change the behaviour of wf_access for a specific windows, you can redefine it at descendent's level.