Android Question Sub Process_Globals not seen in another module?

Martincg

Member
Licensed User
Longtime User
I have a "Main" module where I have
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim pageID As Int              'I can access pageID from another module
   Dim usocket As UDPSocket  'I cannot access usocket from another module
.
.
End Sub

Then I have another module called "Terminal" where I also want to use usocket.

Any reference to usocket in the Terminal module gives an error-

"Error description: Undeclared variable 'usocket' is used before it was assigned any value."

I tried to initialize usocket in Terminal/Activity_Create but the same error.

I understood that a variable declared in Process_Global was available in all modules without having to declare it again so there is something I don't understand about this.

pageID which is also declared in Sub Process_Globals can be used in another module as I expected.

Can someone tell me what is different about certain variable or objects which causes this problem and what I should do to get round it?
 

hillzx

Member
Licensed User
Longtime User
As i know, you can make a code module, write something like this Public myVar as string, then you can call it from everywhere with modulename.myvar
 
Upvote 0

Martincg

Member
Licensed User
Longtime User
Oh, I see, you have to prefix the variable with the name of the module where it was declared.

Thank you hillzx, that wasn't at all obvious to me and means it is not what I would call a global variable. Still that's irrelevant because I know what to do now.

But I still don't understand why the prefix is needed with usocket but not with the pageID int.
Cheers. Can you tell me why that is?
 
Last edited:
Upvote 0

Martincg

Member
Licensed User
Longtime User
OK, thanks Erel, I'll remember that, but I am certain I didn't have another usocket variable in the other module and it compiled ok. Also I'm certain that having the same Process global declared in 2 modules doesn't cause an error but I think it should at least give a warning because it is likely to be a mistake.

Even so, after I added "Main." before every usocket in the other module I still had a problem.
In the Main module the usocket is declared and in Activity_Create it is initialized to give the interrupt handler and the port. I used 0 for the port here.

usocket.initialize("usocket",0,100)'there is Sub usocket_PacketArrived in this module

In the my Terminal module I first found that I couldn't handle received udp packets, then I did this in ACtivity create

Main.usocket.initialize("usocket2", Main.usocket.port,100) 'because I want the same port throughout the process. There is Sub usocket2_PacketArrived in this module

That worked, and I could send and receive packets in the Main activity and then go to the terminal activity and keep using the same port. But when I go back to the first activity again nothing I could come up with would work and all the packets received were queued up until I went back to the terminal activity. Can interrupts for an activity work when another is running? Well stupid question I suppose because apparently they can.

Eventually I gave up and had 2 separate UDP sockets and that let my app work but now the server has to reply to 2 different ports. That's not a problem but my lack of understanding of this is (to me).

Can you suggest a solution? Is having a global object is a problem when it comes to dealing with interrupts?
 
Last edited:
Upvote 0
Top