Page 1 of 1

Evaluate Macro parameter before macro execution?

Posted: Sun Jan 31, 2016 9:00 pm
by Ramses800
I'm trying to figure out how to feed a variable to a macro and use it to assemble it into another variable name inside the macro. The ting is , I would like call the macro inside a loop like below but the parameter used in the concantenated variable name in the macro is the literal value of the variable not its content even if it would seem that it should work? Is there any way to do this?

Code: Select all

Macro TestD(number)
  Debug "Macro input parameter:" + number
  Debug "Variable value :" + Str(i#number)
EndMacro

i1=2
i2=4
i3=6
; Works
TestD(1)
TestD(2)
TestD(3)
; Does not work
For i=1 To 3
  Testd(i)
Next
results in

Code: Select all

[20:55:59] Waiting for executable to start...
[20:55:59] Executable type: Windows - x86  (32bit, Thread)
[20:55:59] Executable started.
[20:55:59] [Debug] Macro input parameter:1
[20:55:59] [Debug] Variable value :2
[20:55:59] [Debug] Macro input parameter:2
[20:55:59] [Debug] Variable value :4
[20:55:59] [Debug] Macro input parameter:3
[20:55:59] [Debug] Variable value :6
[20:55:59] [Debug] Macro input parameter:1
[20:55:59] [Debug] Variable value :0
[20:55:59] [Debug] Macro input parameter:2
[20:55:59] [Debug] Variable value :0
[20:55:59] [Debug] Macro input parameter:3
[20:55:59] [Debug] Variable value :0
[20:55:59] The Program execution has finished.

Re: Evaluate Macro parameter before macro execution?

Posted: Sun Jan 31, 2016 11:30 pm
by skywalk
Macros are expanded at compile time so only constants are known.
Your variable i = 0 at this time.
Use a Procedure(). :wink:

Re: Evaluate Macro parameter before macro execution?

Posted: Sun Jan 31, 2016 11:34 pm
by Demivec
You are trying to reference a variable with a dynamic value (one that is determine at runtime). Macros only function at compile time.

Another option is to use an array instead that you store your values in and index as you need them. If you need more diverse names to be accessed you can declare those variables with 'Runtime' and use the appropriate functions to access them from the Runtime library.

Re: Evaluate Macro parameter before macro execution?

Posted: Sun Jan 31, 2016 11:35 pm
by STARGÅTE
A macro is a compiler function.
Your For-loop is a run time function, so the macro cannot know the value of "i" and the macro is translate to:
Debug "Variable value :" + Str(ii)
because, the "i" is insert for "#number"

There is no chance to to get i1, i2 and i3 from a macro which called only one time (on the compiler!) in your loop.

Re: Evaluate Macro parameter before macro execution?

Posted: Mon Feb 01, 2016 7:26 am
by Ramses800
Thanks for the replies!
But the thing that is confusing me, seeing the outputted results, it seems that the macro indeed can use the incremented value in this part

Code: Select all

Debug "Macro input parameter:" + number
but not in this part

Code: Select all

Debug "Variable value :" + Str(i#number)

Re: Evaluate Macro parameter before macro execution?

Posted: Mon Feb 01, 2016 7:34 am
by STARGÅTE
Your macro:

Code: Select all

Macro TestD(number)
  Debug "Macro input parameter:" + number
  Debug "Variable value :" + Str(i#number)
EndMacro
and this code:

Code: Select all

For i=1 To 3
  Testd(i)
Next
is translated to this result:

Code: Select all

For i=1 To 3
  Debug "Macro input parameter:" + i
  Debug "Variable value :" + Str(ii)
Next
Note the "ii" in the second line, this variable name doesn't exist.

Re: Evaluate Macro parameter before macro execution?

Posted: Mon Feb 01, 2016 8:23 am
by Ramses800
Thanks stargate, that clearifies it to me!