Jump to content
Macro Express Forums
terrypin

Using variables within main and submacros

Recommended Posts

I suspect that I might have some fundamental misunderstanding about using global variables. I've spent many hours trying to understand why the following doesn't work. (I've simplified the names.)

 

My main macro Includes eight submacros (some with subsubmacros), interspersed with other commands. Some 30 variables are captured by the main macro and the submacros from a variety of applications. Like this simplified example:

 

Main macro starts

Captures variable V1

Captures variable V2

Macro Run Submacro-A

---Captures variable V3

Capture variable V4

Macro Run Submacro-B

---Captures variable V5

Text Box Display: Show values of V1, V2...V5. All look OK

Macro Run Submacro-C

---Text Box Display: Show values of V1, V2...V5. Some values are missing!

 

As I understand the documentation, I do not need to use Variable Save and Variable restore commands in either the main or submacros. That is apparently only necessary if I'm running the macros 'independently'. But in case I'm wrong about that, I have tried doings so - still with no success. (No combination so far gives me a full set of results at the final stage.)

 

Anyone have any insights or flashes of inspiration about this please?

 

Or answers to a few specific questions:

 

What exactly gets saved by Variable Save? And what gets restored by Variable Restore? If you do the latter twice in succession, what should be the result? MX Pro Help seems rather sparse on this subject once you step beyond the simplest of usage.

 

Terry, East Grinstead, UK

Share this post


Link to post
Share on other sites

http://bluepointdesign.com/macros/GlobalLocal/Default.aspx

I'm pretty sure I've shared this with you before. We discussed this long ago and most of us came to the conclusion that one should never use variable save to get around scope issues. In every case we considered it was simply a matter of people not understanding scope so I wrote this white paper. I've written hundreds of macros and often use sub-macros and once I understood scope I never used Variable Save/Restore again.

Share this post


Link to post
Share on other sites

Hi Cory,

 

As it was a couple of weeks after my post that you replied, just before Christmas when I was away, I've only just seen it.

 

Yes, you're right, we did discuss it a long time ago. But it doesn't really help answer my question of 10th December. And after studying your article again today I recall why I never did get the hang of it. It's because one of your 'rules' is not right.

 

"4. All variables created by a macro cease to exist when the macro is done."

 

But they spring to life again after a Variable Restore!

 

That command restores all variables (of the specified type), in all macros in the current session of MX.

 

Suppose macro A uses the variable %T(1)% and that is used in macros B, C, D etc elsewhere in your macro library. If %T(1)% is set early in macro A and a Variable Restore is used later in the macro, %T(1)% will be changed back to its value before the last Variable Save.

 

In fact that's essentially why I was geting those confusing results I described. The actual macro is complex (30 variables, 8 submacros called) but I've just returned to it and hopefully will soon have it mastered!

Share this post


Link to post
Share on other sites

I don't use Variable Save or Restore. I consider it a bad practice. I wasn't considering that command in the context of my statement so I am not wrong.

Share this post


Link to post
Share on other sites

I don't think we disagree on anything basic here. I think Variable Save/Restore commands are at best almost entirely redundant and at worst capable of leading to very confusing results.

 

But I also think your 'rule' could be misleading unless modified. Maybe by adding something like: "This assumes no variable/restore commands have been used in the same MX run session."

 

-- Terry, East Grinstead, UK

Share this post


Link to post
Share on other sites

I don't use Variable Save or Restore. I consider it a bad practice. I wasn't considering that command in the context of my statement so I am not wrong.

Unless one invokes a macro from the command line and wishes to pass pre-initialized variables, in which case Variable Restore is mandatory.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×