Cannot access Global variables in code module

raytronixsystems

Active Member
Licensed User
Longtime User
Hello!

I know this topic has been beaten to death here, I've tried all of the things that people have suggested but still cannot access any global variables defined in the main activity Process_Global section from anywhere else other than the main module. I have include some sample code with one variable as an example.

code:

Main: ' this is code in main module

Sub Process_Globals
Dim TestVar As String: TestVar = "1234" ' test global variable
'
' other stuff goes here....
End Sub


My second code module looks similiar to this:

CommSubs: ' this is the name of my second module

Sub TestSub ' this subroutine just for testing compiler
Dim LocalVar As String ' define a local variable

LocalVar = TestVar ' this generates a compiler error
' note after I comment out the previous line the next line is active:
LocalVar = Main.TestVar ' this also generates the same compiler error
'
End Sub

No matter what I've tried I cannot reference the globals in the second module so I had to cut and paste the code into the main module and everything works as expected. I thought that prefixing the variable with
Main would have fixed my problem. I've tried Strings, Int's and upper and lower case main prefixes but all react the same. I am running the latest IDE v1.17 and not sure what I should do since I have several dozen variables defined in my main module that have to be accessed by other code modules. Passing variables to subroutines in other modules works as expected but all of the variables defined in my main module are psuedo constants like file names, etc. Sorry for the way the code looks on here. I don't know how to use the code tags yet.

Any help would greatly be appreciated!

thx,
Ray
 

raytronixsystems

Active Member
Licensed User
Longtime User
Uploaded code and screen capture of my error

Hi Erel!

Thanks for the fast reply (as usual!) I have uploaded a zip file of a test program demonstrating the error and a .jpg of the screen capture of what I am seeing here. I eliminated all of the rest of the code in order to make it as simple as possible to see what I am experiencing on my end. Hopefuly you will see what I see here.
Thanks in Advance for your help
Ray
 

Attachments

  • TestPgm.zip
    5.8 KB · Views: 209
  • TestPgmCompilerMsg.jpg
    TestPgmCompilerMsg.jpg
    34.9 KB · Views: 370
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

The problems looks to be that you've commented out part of the default Code Module:

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

Re-enable the Sub Process_Globals and your app compiles without error:

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

Martin.
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
Thx warwound and Erel but I am a bit confused: whenever one creates a new static code module the IDE creates a template for the module with a
Process_globals section at the top. I did as warwound had suggested in my TestPgm. I uncommented out the Process_Globals section in the CommSubs static code module of TestPgm and the compiler error went away. In the actual project which I am developing, I had removed this section altogether from my CommSubs module assuming that since I only had ONE activity in my project and that all of my globals for that activity need to be declared in the 'main' module. I have not found anything up here to suggest that multiple Process_Globals sections are required in order to reference globals defned in main and assumed that I could remove that section altogether from additional static code modules even though the IDE put them there from the get go.

Anyways, I added Process_globals section back to my "real" project and NO COMPILER errors anymore so now I can begin to split off all of the subroutines from main back into CommSubs since things will now work.

If one needs a Process_Globals section in static code modules what would you put there and how would you reference them if you only have ONE Activity? I come from over 30 years of C/Assembler programming background where I would declare all constants and Globals in one module and not have them all scattered about in my applicarion. All constants are given names and assigned a value, This is good programming practice - I name all of my constants and never declare (i.e Timer_interval = 1000, etc.)them in subroutines scattered throughout the code. If One needs to change a constant he does not have to look all over the place in the code. It is changed in one place and all references are updated from that central location

So I am assuming the multiple Process_globals sections issue might/could be a compiler bug right now and not worry about it for the time being. I ran into something else the other day where I had mispelled a variable name and the compiler did not pick it up at all. As I recall, it was not a matter of an upper/lower case issue - the variable was totally spelled wrong and the compiler ignored it totally . I should have documented it right away and reported it but instead fixed it,

My first project is getting larger now - near 1500 lines involving files, encryption, splash screens and probably will use every library available here LOL before it's completed. This will be my first real app so i might as well dive in head first to learn the language. This is how I've always learned a new language - pick a project and then pick and learn the select language to complete it. I don't know SQL and that will be coming soon too. I love B4A so far and looking forward to start coding the next dozen or so I have on the back burner once this is released to the Market. Hopefully, I will be considered an "expert" by then! BTW I am using v1.7 instead of 1.17 as I had initially reported.


thx for your help. Maybe you others here can benefit from my discovery.
-Ray:D
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
thank you!

All is working well now that I had reactivated Process_globals subroutine in my static code module. I had moved 4 important subroutines there yesterday and successfully referenced all of the globals I had need from my main module.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Rats! I had high hopes that this thread was going to solve the same problem which I have been having, but I never had Process_Globals commented out in the module, so that isn't my problem.

In the Module, every place I try to access a Main variable (e.g.: If Main.SomeVar...), compiling gives the error: "Unknown member: SomeVar").

Before putting "Main." in front of "SomeVar", the variable name is red, indicating that it has not been Dimmed. When I add "Main." to the front, it turns to white, indicating that it has been Dimmed.

However, if I put If Main.NeverDimmedVar..., it also turns white even though no such variable exists in Main, so that is a second problem.

The main problem is: How do I access a Main variable from a Sub?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If you write
B4X:
main.
doesn't the auto-complete give you the desired variable?
About your second note, it's true, I've noticed it too, a variable turns white even if it doesn't exist. But I don't give to this importance since I'm always getting variables using 'main.' and the ide suggests existing ones.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
If you write
B4X:
main.
doesn't the auto-complete give you the desired variable?
About your second note, it's true, I've noticed it too, a variable turns white even if it doesn't exist. But I don't give to this importance since I'm always getting variables using 'main.' and the ide suggests existing ones.

The problem is that I had an app that apparently got so big that Debug quit working so Erel said to put some of the subs in a code module. After doing that, I just went through prefixing all the variables with "Main.", so I wasn't typing in new code like you are talking about.

So I went back to the Sub and just started typing in "Main." and recognized that only the variables and views from Main's Process_Globals were listed, and I had not moved the variables needed by the code module from Main's Globals to Main's Process_Globals, which I am doing now. Shouldn't take more than a few hours to get that straightened out with the hundreds of variables and views Dimmed there. (Sigh...)

Thanks for putting me on track.:icon_clap:
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Global Variables

Is there a way to declare global (public) variables w/o always having to include the module name first?
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi guys, I am having a similar problem adding code modules.
In globuls (main) I have Dimmed a variable scvMenu as Scrollview

When I try to access this in the new code module, I cannot access it at all? Even when adding main. if front of it?

It's just a very small test routine to try and add panels in one routine as before I was doing almost the same thing for every page.

Any ideas?

Malky
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You cannot access activity views directly from a code module, and activity views cannot be declared in Process:Globals.
But you can submit views in a routine call.
Main:
MyModule.ScrollViewAddItem(scvMenu, Panel)
Code module MyModule:
Sub ScrollViewAddItem(scv As ScrollView, pnl As Panel)

End Sub
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus, been a long time. Thank you very much, tried a quick test and successfully added a panel.

I'll try it with the complete list next.

I assume, I can use the same function passing another scrollview name, i.e. Languages.

Malky
 
Upvote 0
Top