Bug? Problem with b4A editor - perfect example

aedwall

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These are Project Globals, "known" by the whole project'
    'Only "Main" has these type of globals in B4Pages
    'In classic Activities, each activity has a Process Global sub'
    'Not all types can be process globals'
    Public ActionBarHomeClicked As Boolean
End Sub

Sub Globals
'These are Module Globals, known only throughout the Module they are declared on
End Sub

The Starter.bas is a "service" that was introduced with B4Pages to ease the entry point specially when coming From Background to foreground (ie: the user sennds your app to background to consult an sms and then comes again into your app)
In the Activities framework, yhe app would be killed by the OS after some time, or when the OS needed the space... The starter service tries to keep the app alive, even if it's in background for long.

You should consult @klaus booklets about B4X, they are accessible from his signature
I have all the booklets, plus the large .pdf file by Wyken Seagrave. I have looked and looked for the right info, and specific examples, but there is so much information available that I cannot find my way through the forest. I have uploaded a test project (in another post) if anyone wants to try their luck. Thank you.

>> Only "Main" has these type of globals in B4Pages

If that is true, then why can't I compile my small project when I do this in "Main"?

Global variables not global variables:
Sub Process_Globals
    Public ActionBarHomeClicked As Boolean

  Public swe0 As SweDate
  Public swe1 As SwissEph

  Public JD, Transit(11), Speed(11) As Double
  Public lnn(11), month_names(13), p_names(11) As String
  Public vrs As String
  Public num_bodies, sel_number, tz_offset As Int
  Public ephem_codes(11) As Long
  Public deltaT_val0(200), deltaT_val1(200), deltaT_val2(200) As Double
  Public ae(2), deltaT As Double
End Sub

I get this error message:

B4A Version: 13.10
Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'swe1' is used before it was assigned any value.
Error occurred on line: 60 (B4XMainPage)
swe1.swe_set_ephe_path(File.DirInternal)
 

Cableguy

Expert
Licensed User
Longtime User
I have all the booklets, plus the large .pdf file by Wyken Seagrave. I have looked and looked for the right info, and specific examples, but there is so much information available that I cannot find my way through the forest. I have uploaded a test project (in another post) if anyone wants to try their luck. Thank you.

>> Only "Main" has these type of globals in B4Pages

If that is true, then why can't I compile my small project when I do this in "Main"?

Global variables not global variables:
Sub Process_Globals
    Public ActionBarHomeClicked As Boolean

  Public swe0 As SweDate
  Public swe1 As SwissEph

  Public JD, Transit(11), Speed(11) As Double
  Public lnn(11), month_names(13), p_names(11) As String
  Public vrs As String
  Public num_bodies, sel_number, tz_offset As Int
  Public ephem_codes(11) As Long
  Public deltaT_val0(200), deltaT_val1(200), deltaT_val2(200) As Double
  Public ae(2), deltaT As Double
End Sub

I get this error message:

B4A Version: 13.10
Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'swe1' is used before it was assigned any value.
Error occurred on line: 60 (B4XMainPage)
swe1.swe_set_ephe_path(File.DirInternal)
as I said, not all types can be placed there... so you should check that too
 

aedwall

Active Member
Licensed User
Longtime User
as I said, not all types can be placed there... so you should check that too
Here is a small program that will NOT compile because it doesn't think that "p_names" has been defined.

sub_routines.bas:
Sub Process_Globals
  'These global variables will be declared once when the application starts.
  'These variables can be accessed from all modules.
'  Dim p_names(9) As String
End Sub


Sub Tell_me_a_planet_name()
  Dim t As String  

  t = p_names(6)
   
  Msgbox("The planet name is " & t, "Planet Name")
End Sub

Test3.b4a:
Sub Process_Globals
  'These global variables will be declared once when the application starts.
  'These variables can be accessed from all modules.
  Dim p_names(9) As String
End Sub

Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
  'Activity.LoadLayout("Layout")
 
  p_names(0) = "Sun"
  p_names(1) = "Moon"
  p_names(2) = "Mercury"
  p_names(3) = "Venus"
  p_names(4) = "Mars"
  p_names(5) = "Jupiter"
  p_names(6) = "Saturn"
  p_names(7) = "Uranus"
  p_names(8) = "Neptune"
 
  sub_routines.Tell_me_a_planet_name()
End Sub

The only way it compiles is if I remove the comment in the top "Sub Process_Globals" code block. But if I do that, then I get no planet name in the message box. It makes no sense to me.
 

Attachments

  • Project.zip
    10 KB · Views: 65

Cableguy

Expert
Licensed User
Longtime User
It compiles for me!!!

1746995464021.png
 

Cableguy

Expert
Licensed User
Longtime User
This works as you want, please study it to understand arrays and how to share values between modules
 

Attachments

  • Test2.zip
    10.3 KB · Views: 60

stevel05

Expert
Licensed User
Longtime User
You are not getting a planet name because of the Program lifecycle. you can see that at the beginning of activity create, B4xPagesManager is initialized, that will create B4xMainPage before you assign the values to P. So your call to Tell_me_a_PlanetName is looking at an array of empty values.

There are several ways you can do this, depending on what else is in your app, but just moving the assignments into the B4xPage_Created in b4xMainPage will solve it for the example you have given.


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'Root.LoadLayout("MainPage")
    Log("Page Created")
 
    Main.p_names(0) = "Sun"
    Main.p_names(1) = "Moon"
    Main.p_names(2) = "Mercury"
    Main.p_names(3) = "Venus"
    Main.p_names(4) = "Mars"
    Main.p_names(5) = "Jupiter"
    Main.p_names(6) = "Saturn"
    Main.p_names(7) = "Uranus"
    Main.p_names(8) = "Neptune"
    sub_routines.Tell_me_a_planet_name()
End Sub

Note the use of Main to point to the global variable in Main.

You also need to do this in the called sub

B4X:
Sub Tell_me_a_planet_name()
    Dim t As String

        t = Main.p_names(6)

    Msgbox("The name of the planet is " & t, "Planet Name")
End Sub
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
You are not getting a planet name because of the Program lifecycle. you can see that at the beginning of activity create, B4xPagesManager is initialized, that will create B4xMainPage before you assign the values to P. So your call to Tell_me_a_PlanetName is looking at an empty array.

There are several ways you can do this, depending on what else is in your app, but just moving the assignments into the B4xPage_Created in b4xMainPage will solve it for the example you have given.


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'Root.LoadLayout("MainPage")
    Log("Page Created")
   
    Main.p_names(0) = "Sun"
    Main.p_names(1) = "Moon"
    Main.p_names(2) = "Mercury"
    Main.p_names(3) = "Venus"
    Main.p_names(4) = "Mars"
    Main.p_names(5) = "Jupiter"
    Main.p_names(6) = "Saturn"
    Main.p_names(7) = "Uranus"
    Main.p_names(8) = "Neptune"
    sub_routines.Tell_me_a_planet_name()
End Sub

Note the use of Main to point to tht global variable.

You also need to do this in the called sub

B4X:
Sub Tell_me_a_planet_name()
    Dim t As String

        t = Main.p_names(6)

    Msgbox("The name of the planet is " & t, "Planet Name")
End Sub
you get here 20secs too late! almost 100% identical to my solution
 

Cableguy

Expert
Licensed User
Longtime User
I prefer your solution though, in this case they are more like constants and won't change.
I only didn't do that in order to not confuse the OP more... but I agree, If the values are not to be variable, then declare as constants ("dim const p_name() as string....")
 

aeric

Expert
Licensed User
Longtime User
Doesn't work. I copied this to "Process_Globals" in the Main module and I cannot compile:

Public swe1 As SwissEph

Error description: Undeclared variable 'swe1' is used before it was assigned any value.
Error occurred on line: 60 (B4XMainPage)
swe1.swe_set_ephe_path(File.DirInternal)
I remind you again to start your question in a new thread in question forum.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
sure seems to me a better solution would be to use a map for this

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    Dim Planets As Map = CreateMap(0 : "Sun", 1 : "Moon",  2 : "Mercury", 3 : "Venus", 4 : "Mars", 5 : "Jupiter", 6 : "Saturn", 7 : "Uranus", 8 : "Neptune")
   
    Tell_me_a_planet_name(Planets, -1)
    Tell_me_a_planet_name(Planets, 3)
    Tell_me_a_planet_name(Planets, 5)
    Tell_me_a_planet_name(Planets, 9)  
End Sub

Sub Tell_me_a_planet_name(Planets As Map, PlanetID As Int)
   
    If  Planets.Size = 0 Then
        Log("No Planets to show")
        Return
    End If
   
    If  PlanetID = -1 Then        '  Randomly pick a planet
        Log($"Planet Randomly Picked is: ${Planets.Get(Rnd(0, Planets.Size))}"$)
        Return
    End If
   
    If  PlanetID < 0 Or PlanetID >= Planets.Size Then
        Log($"Invalid Planet ID:${PlanetID} - Max Planet ID: ${Planets.Size-1}"$)
        Return
    End If
   
    Log($"Planet for PlanetID: ${PlanetID}  is  ${Planets.Get(PlanetID)}"$)
End Sub



BUT for the problem with the B4A Editor - I once had a memory chip problem and every so often the editor would flake out. Only after 2 years of this did I realize the problem would only happen when I had a lot of things running. Finally ran a memory check and found the bad chip. Problem with B4A went away and has never returned.
 

aedwall

Active Member
Licensed User
Longtime User
This works as you want, please study it to understand arrays and how to share values between modules
Thank you. Somehow I stopped getting notifications that people were responding to posts. I am trying to catch up now.
 
Top