Android Question Undeclared variable 'RunLog' is used before it was assigned a value

RB Smissaert

Well-Known Member
Licensed User
Have a simple B4A app with 2 modules, Main and Starter. Both need to do logging, but only in debugging mode. I have a Public variable for this bLog, which I have declared in the Starter module like this:

B4X:
Sub Process_Globals
Public bLog As Boolean
End Sub

The Sub that does the logging is in the Starter module:

B4X:
Sub RunLog(strLogText As String)
If bLog Then
Log(strLogText)
End If
End Sub

Now I thought that Sub RunLog would be visible to both modules, so there would be no need to add
this Sub to both modules, but I get:
Undeclared variable 'RunLog' is used before it was assigned a value
if that sub is left out of either module.

In the Main module I have to do:

B4X:
Sub RunLog(strLogText As String)
If Starter.bLog Then '<<<< note Starter here
Log(strLogText)
End If
End Sub

bLog is set at the very first opportunity in the Starter module:

B4X:
Sub Service_Create
bLog = r.GetStaticField("anywheresoftware.b4a.BA", "debugMode")


Is there a way to avoid duplication of this Sub?


RBS
 

LucaMs

Expert
Licensed User
This happens because you can not call a routine in that way ([service name].[routine name]) if the routine is in a Service.

You should use CallSub2 (CallSub2(Starter, "RunLog", "your message")) or CallSubDelayed2 or, better I think, move the routine to a Code Module.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
This happens because you can not call a routine in that way ([service name].[routine name]) if the routine is in a Service.

You should use CallSub2 (CallSub2(Starter, "RunLog", "your message")) or CallSubDelayed2 or, better I think, move the routine to a Code Module.

OK, thanks will move it to code module. In fact I thought it was in a code module, but I can see now that these are different things.

RBS
 
Upvote 0
Top