Jump to content
Macro Express Forums

Variable Save/Restore often unnecessary?


Recommended Posts

While I suppose it's a good discipline to use them, am I correct that the Variable Save and Restore commands are redundant when processing sub-macros in immediate succession, as in this basic example?

 

 

Test integer values
Variable Set Integer %N[1]% to 0
Variable Set Integer %N[2]% to 0
Variable Set Integer %N[3]% to 0
Variable Set Integer %N[4]% to 0
Variable Set Integer %N[5]% to 0
Variable Set Integer %N[99]% to 0
Macro Run: Set new integer values
// Note: No Variable Restore.
Text Box Display: Some integer values

Set new integer values
Variable Set Integer %N[1]% to 111
Variable Set Integer %N[2]% to 222
Variable Set Integer %N[3]% to 333
Variable Set Integer %N[4]% to 444
Variable Set Integer %N[5]% to 555
Variable Set Integer %N[99]% to 999
// Note: No Variable Save


The new values are displayed anyway, despite the absence of save and restore commands.

 

 

--
Terry, East Grinstead, UK

Link to comment
Share on other sites

I do not believe saving and restoring variables is a good practice. The best practice is to use scope properly and unique variable names. I believe the commands are only a hold over to the days when we had a very small quantity of variables. I would only ever use it as a quick (lazy) fix when utilizing old code. But even than it's so easy to simply uncheck the scope box (Make this variable available to macros called by this macro). When I define a variable I almost always uncheck this box. The only exception is when I really want to pass a variable to a sub-macro and/or read one back.

 

If you're not familiar with scope I suggest you read this Wikipedia article but the basic idea is that variables only exist within a unit of code or they exist in all. The terms are local and global. In proper programming languages scope is a really big consideration and very handy as the unit could be as simple as a loop. EG a counter only exists in that loop so when you do another loop later you can use the same variable name because each only exist on their own unit of code. In MEP scopes unit is a macro so it's less useful but still useful. But you need to be aware MEP scope has some eccentricities. You might want to read my article about it here.

 

You are correct that it is redundant in your example. But I don't think you understand the intended use. Save/restore is intended when your submacro uses the variable of the same name. EG I might have a variable named %Name% and call a macro in which is coincidently another variable named %Name%. But I don't want my submacro to change the variable in the parent macro. In this case I could save the variables, execute the submacro, and then restore the variables. But at a minimum you have to save all of the defined macros of type which must cost a but of performance. Also what if there are string variables you want to pass back? Much better to use scope.

Link to comment
Share on other sites

Thanks Cory. I haven't studied the subtleties of this 'scope' stuff but will do so. But my question was at a much simpler level. It arose while trying to isolate unexpected results in a macro that used N[1], one of the few old style built in variables I use. It's more easily set as a counter in a Repeat Start command, for example, than say 'nCounter'. I also use N[99] in Repeat Until to get indefinite looping until stopped by other logic.

 

In that macro I found that N[1] had weird values instead of the expected 1, 2, 3, etc. I traced them to another very old macro I'd run earlier which happened to use N[1] in a Get Mouse Position command.

 

I still don't fully grasp why earlier values are accessible in macro X if they have not been saved in macro Y and restored in X. What are the Save/Restore commands meant for if not that?

 

BTW, I prefer using the registry for my most frequently used variables.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

If macro A does a Save Variables and sometime later macro B does a Restore Variables the variables in Macro B will contain values from Macro A. Sometimes this is intentional and needed but from what I have seen, it is more often unnecessary.

 

When a macro is activated it receives its own copy of variables. In other words, all its variables are empty. If variables are defined as 'Global' they are passed to other macros called from the top level macro via a Macro Run command. The Save Variables and Restore Variables commands are not needed for this to happen. When a variable is not defined as Global the value of that variable is not passed to a macro called by the Macro Run command.

 

The Save/Restore Variables command pair is one way to pass variables from one macro to another but it is not necessarily the best way. The Restore Variables command can be used to retrieve variables passed to a macro via the command line.

Link to comment
Share on other sites

If you pass variable values when calling a macro, as in:

C:\Program Files (x86)\Macro Express Pro\MeProc.exe /AMyMacro /VMyString:Now is the time

then you must use the Restore [Text Variables] command in order for the variable MyString to become initialized with "Now is the time".

What I do in such cases is to end the macro with a Clear Text variables command, followed by a Variable Save Text Variables command so that I don't subsequently inherit a value I no longer want.

Apart from this, i cannot find a use for the Save and Restore commands in MEP.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...