Android Question How to define code module variable?

tonyp

Member
Licensed User
Longtime User
I need to define a variable that is accessible by several routines in the code module, but not outside the code module. Other than playing with the name to try to hide it (which I don't like as the same name could eventually collide with another code module's), is there another way?
 

DonManfred

Expert
Licensed User
Longtime User
use can use a map (defined as private) and inside this map you can create your "variables"...
B4X:
dim mymap as map
mymap.initialize
mymap.put("myname1","myvalue1  (or even a object instead of this string))
mymap.put("myname2","myvalue1  (or even a object instead of this string))
'
'
'
log(mymap.get("myname1"))
 
Upvote 0

tonyp

Member
Licensed User
Longtime User
Still having problems with this. I tried:

B4X:
Sub Process_Globals
  'These global variables will be declared once when the application starts.
  'These variables can be accessed from all modules.
  Private act As Activity
End Sub

and get this error:
Cannot access activity object from sub Process_Globals

but this is allowed:

B4X:
Sub SomeSub(act As Activity)
  act.Finish
End Sub

Why the first one is not allowed? So, again the question is:
How can I declare a variable (in this case of type Activity) that is visible in the whole module but not outside it?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Codemodule does not have an activity or can handle them. It´s a code-module and as far as i know codemodule can not hold an activity.

But you can declare an object in globals (not process globals) and declare it as private. It will be visible in the hole code module.
Try to move the dim to sub globals
 
Upvote 0

tonyp

Member
Licensed User
Longtime User
@DonManfred I appreciate your response but ...

A code module does not seem to have a Sub Globals, only Process_Globals, so how do you propose I do that? (Creating a new code module shows me only Sub Process_Globals is all that's there.)

I also think I read you cannot have an activity in code modules. But, to me this seems to be inconsistent with allowing an activity to be passed as parameter to a routine in code module. If it can access an activity from a subroutine, it should be able to accept it for definition. Regardless, ...

@Erel Maybe you can shed some light on this general problem. Take the licensing code only as an example, and try to make it a shared library, therefore, code module. All apps can now use this common library by only passing their 'self' as parameter.

There are four routines involved: ValidateUser(), lc_Allow, lc_DontAllow, and lc_Error. The ValidateUser() can have an extra parameter added to it which is the activity to use. The other three are call-backs from the library, and therefore cannot be altered (without messing with the library). So, the problem now is how to tell lc_DontAllow (for example) which activity to Finish. The only solution I could up with is to save the Activity passed in ValidateUser() extra argument in a variable (visible only inside this shared library code module), and then use that variable inside lc_DontAllow to run the Finish method the value having been set by the ValidateUser() sub.

So, is there a solution to this problem? (A general one, I mean. Don't think of it only in terms of this licensing example, as it applies to a whole lot more cases where you want to have shared code library deal with objects passed in from non-shared code in main application.)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@DonManfred I appreciate your response but ...

A code module does not seem to have a Sub Globals, only Process_Globals, so how do you propose I do that? (Creating a new code module shows me only Sub Process_Globals is all that's there.)

I´m sorry, my fault... I have confused things... You are surely right. Code module have no globals sub...

You should use a Class-Module like @Erel states. Master Yoda Erel is always right
 
Last edited:
Upvote 0

tonyp

Member
Licensed User
Longtime User
@Erel Thanks, problems solved! My misconception was that shared library code could only be code modules. Apparently it can anything.

This is what got me "Shared code modules are modules ..." because I didn't pay attention to the parentheses that followed.

Thanks to all.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…