Android Question Question about variables

kepler

Active Member
Licensed User
Good morning,

As you know, I'm facing some troubles with Global vars.
The fact is that if I have a var in a code module, for instance, I can change it.

But here's the problem: supose I have a string var called Data in Module1 with the value "None".
If I, in the Activity_Create, put something like this:

B4X:
Module1.Data = "777"
Msgbox(Module1.Data,"Info")
I get the value 777. Ok.

If I create a Sub, say Function1, and include it like this:

B4X:
Sub Function1 as Int
Module1.Data = "777"
Msgbox(Module1.Data,"Info")
End Sub
I also get 777.

BUT, supose I do this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
dim res as Int = Function1
Msgbox(Module1.Data,"Info")
End Sub

Sub Function1 as Int
Module1.Data = "777"
End Sub
I'll get 777 IF the routine is as it is. But I'll get None, if Function1 is a "catch" function, like for example:

Sub JobDone (Job As HttpJob)
Module1.Data = "777"
...

or

Sub MyWifi_FoundLocation ()
Module1.Data = "777"
...

In these cases, it seems that there's no time for the Function1 assign the new value to the var.
And Activity_Create doesn't catch it - perhaps because it runs aside Function1 at the same time?

Maybe I'm seeing this wrong.

Kind regards,

Kepler
 

sorex

Expert
Licensed User
I think the int declaration method you use is only for static values, not to call a function/sub.

try it like this instead

B4X:
Sub Activity_Create(FirstTime As Boolean)
dim res as Int
res = Function1
Msgbox(Module1.Data,"Info")
End Sub
 

kepler

Active Member
Licensed User
Yes, I'm aware of that.
But how do I catch the values of the routines like Sub MyWifi_FoundLocation () or Sub JobDone (Job As HttpJob)?

Kepler
 

DonManfred

Expert
Licensed User
You cannot "Catch" a result from JobDone as you DONT call it. What you can do is to call a sub in your activity FROM Jobdone to GIVE the value to your sub.
I dont use wifi-libs so i dont know MyWifi_FoundLocation () and what it does... But i believe it is the same like Jobdone... You need to call your sub giving the value from that sub too
 

sorex

Expert
Licensed User
you either store them in the module/class or you call a sub in your main module with callsub2(modulename,"subname","yourvalue")
 

kepler

Active Member
Licensed User
Hi,

Actually there's a process - not very nice though - to try to "catch" the variables.
Using a Timer.

You fire the Timer in Activity_Create. Create a number of beats too - that you count in Timer1_Tick sub.
In the subs that take long, declare the new value obtained to a Global variable.
In Timer1_Tick, just verify if the default Global variables have change - for the time that you want to wait. In the refered subs I mentioned, about 1.5 seconds were enough. If the values don't change, deal the best way - the subs might be jammed.

It's the best solution I've found.

Cheers,

Kepler
 
Top