MacroExpandedCount question

Just starting out? Need help? Post your questions and find answers here.
lule
User
User
Posts: 31
Joined: Fri Sep 17, 2010 8:22 pm

MacroExpandedCount question

Post by lule »

Hello,
I've got problem with MacroExpandedCount function

Code: Select all

Macro _hGadget
	MacroExpandedCount
EndMacro

;Example 1
Debug "Example1"
Global z1 = _hGadget
Global z2 = _hGadget
Global z3 = _hGadget
Global z4 = _hGadget

Debug z1
Debug z2
Debug z3
Debug z4

;Example 2
Debug "Example2"
Global Dim az.l(4), i
For i = 0 To 3
	az(i) = _hGadget ;<- HERE ERROR AFTER THE FIRST LOOP
Next

For i = 0 To 3
	Debug az(i)
Next
Result :

Example1
1
2
3
4
Example2
5
5
5
5

In case of Array (Example 2) the macro seems not running after the first loop?
Is it a bug or i am doing something wrong?
Linux Mint 21.1 Vera base: Ubuntu 22.04 jammy.
PureBasic 6.10 LTS (Linux - x64)
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: MacroExpandedCount question

Post by Thunder93 »

When using in loops. It'll only record the initial usage but not keep counting. The way Macro's works.

For instance have a look at the following;

Code: Select all

Macro _hGadget
  MacroExpandedCount
EndMacro

For ii = 0 To 5
  Debug _hGadget
Next : Debug ""

For ii = 0 To 5
  Debug _hGadget
Next
Last edited by Thunder93 on Fri Mar 17, 2017 6:18 pm, edited 2 times in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: MacroExpandedCount question

Post by spikey »

You're doing something wrong - you can't use MacroExpandedCount in the way you are in your second example, or at least you can't use it to get the result you were expecting anyway.

The MacroExpandedCount is handled by the macro preprocessor when the code is compiled, not at runtime. The result is correct because the macro is only expanded once in that loop in the compilation process. The loop executes more than once at runtime but this is irrelevant.
lule
User
User
Posts: 31
Joined: Fri Sep 17, 2010 8:22 pm

Re: MacroExpandedCount question

Post by lule »

Thanks for answers.

I forgot it on every macro's use.
I have replaced macro by a procedure with static var.
Linux Mint 21.1 Vera base: Ubuntu 22.04 jammy.
PureBasic 6.10 LTS (Linux - x64)
Post Reply