Scope of Local Objects in Sub

BPak

Active Member
Licensed User
Longtime User
I have been studying the XmlSax and found a forum link by SpaceCow.

http://www.b4x.com/forum/basic4android-updates-questions/20246-xmlsax-read-sublist.html

I made the program with B4A and it works fine. However, on studying it in detail I got lost with the Scope of the Objects used in Erol's example.

Documentation says:
Scope

Variables that are declared in Sub Globals or Sub Process_Globals are global and can be accessed from all subs.
Other variables are local and can only be accessed from the sub that they are declared in.

The tempDay Local variable in Sub Parser_StartElement is Dimmed and added the the global MyDays List.

After leaving the Sub Parser_StartElement the scope of the tempDay Local var is gone...

B4X:
If Name = "day" Then        
Dim tempDay As Day 'this creates a new Day object        
tempDay.Initialize        
tempDay.Activities.Initialize        
MyDays.Add(tempDay)

Though in the Sub Parser_EndElement the Text for the Elements is assigned to the tempDay GLOBAL variable.

I do not understand how this then gets into the MyDays List as tempDay (global) is not assigned to an item in MyDays.

Could someone explain how the data gets into the tempDay Local Variable which was added to the MyDay list??

:sign0104:
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
tempDay is not a local variable. It's a global variable which is being redimmed inside the first sub, in order to get new data.
 
Upvote 0

BPak

Active Member
Licensed User
Longtime User
Thanks mc73.

B4X:
Dim tempDay As Day [B]'this creates a new Day object[/B]

So that code does not create a NEW Day Object LOCALLY.

It resets the Global Object?



.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Exactly. Try for example, dimming tempDay as a stringm in Sub Parser_StartElement. You'll get an error.
 
Upvote 0

BPak

Active Member
Licensed User
Longtime User
Ok. Will do that when i get home.

There can only be one tempDay and it is global.

This is very different to what I am used to in other langs.
 
Upvote 0

BPak

Active Member
Licensed User
Longtime User
Correct does give an error...

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Type Bags(TypeBag As String, NumberBags As Int)
   
   Dim MyBags As Bags
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.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Hoofit
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Hoofit
   Dim MyBags As Int
End Sub

ERROR...

Compiling code. Error
Error compiling program.
Error description: Current declaration does not match previous one.
Previous: {Type=Bags,Rank=0}
Current: {Type=Int,Rank=0}
Occurred on line: 29
Dim MyBags As Int
 
Upvote 0
Top