Use of macro's in modules

Just starting out? Need help? Post your questions and find answers here.
Geert
User
User
Posts: 47
Joined: Thu Aug 16, 2012 3:17 pm
Location: Belgium
Contact:

Use of macro's in modules

Post by Geert »

I"m getting some weird results when using macro's in modules.
When a macro defines a global variable, it doesn't seem to act like one.
Can somebody explain? Or is this a bug?

(Tested on MacOSX and Windows with PB5.24/5.31: same results.)

Code: Select all

DeclareModule TestGlobal
 Macro DefineGlobal()
  Global a=10
 EndMacro
 Global b=9
EndDeclareModule

Module TestGlobal
EndModule

UseModule TestGlobal

Procedure Test()
 ;Global a         ;Uncomment this and the results are as expected: a=10
 Debug "a="+Str(a) ;Why 0? I expect 10, because a is defined global.
 Debug "b="+Str(b)
 a=8
 b=7
EndProcedure

DefineGlobal()

Debug "a="+Str(a)
Debug "b="+Str(b)

Test()

Debug "a="+Str(a)
Debug "b="+Str(b)

;Debugger results:
; a=10
; b=9
; a=0    ??? why not 10?
; b=9
; a=10   ??? why not 8?
; b=7
Visit my website: https://www.basic-apps.com

Home of SoftMaths, ToolsForIcons, Checksums, MoonPhases, ...
Fred
Administrator
Administrator
Posts: 18344
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Use of macro's in modules

Post by Fred »

This is not a macro issue, you are defining your global variable after your procedure definition, so it's not taken in account.

Code: Select all

DeclareModule TestGlobal
 Macro DefineGlobal()
  Global a=10
 EndMacro
 Global b=9
EndDeclareModule

Module TestGlobal
EndModule

UseModule TestGlobal

DefineGlobal()

Procedure Test()
 ;Global a         ;Uncomment this and the results are as expected: a=10
 Debug "a="+Str(a) ;Why 0? I expect 10, because a is defined global.
 Debug "b="+Str(b)
 a=8
 b=7
EndProcedure


Debug "a="+Str(a)
Debug "b="+Str(b)

Test()

Debug "a="+Str(a)
Debug "b="+Str(b)

;Debugger results:
; a=10
; b=9
; a=0    ??? why not 10?
; b=9
; a=10   ??? why not 8?
; b=7
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Use of macro's in modules

Post by infratec »

Always remember:

PB is a single pass compiler.
So you need an 'order'.
Geert
User
User
Posts: 47
Joined: Thu Aug 16, 2012 3:17 pm
Location: Belgium
Contact:

Re: Use of macro's in modules

Post by Geert »

Sometimes it's so obvious... Thanks!
Visit my website: https://www.basic-apps.com

Home of SoftMaths, ToolsForIcons, Checksums, MoonPhases, ...
Post Reply