I'm having trouble understanding the behavior exhibited in the attached code. If you really understand how to use the "map" type, perhaps you could help. My question is embedded in the second bullet below. Here's the situation:
* Although all three subs (Sub2, 3, and 4) are similar in structure, please notice that each also has its own set of similarly defined variables. So they are not using the variables from another sub.
* Sub3 is virtually identical in structure to Sub2, EXCEPT for the addition of a .put statement that sets a value in the (supposedly empty/clear) map to "" (or null?). To see this notice that line 33 is commented out while line 57 is not. The question is, why does the addition of this statement affect the log entries that follow this statement, as compared to the corresponding log entries from Sub2? That is, if the map was already "cleared," why aren't log lines 303 and 304 identical to log lines 203 and 204. After all, the .put statement is supposed to return a null (not the string "null"), when I "put" a new key-value pair in an empty map, isn't it?
* Sub4 is similar to Sub3, but the two .put statements have been swapped. The logs for Sub4 are not surprising, at least not in light of Subs2 and 3. I included it for comparison purposes only.
Here is the code and the resulting log. The subroutine CallOtherSubs gets called from Main; and that's really all that Main does (e.g., no variables are dim'd in Main). The code is also attached (although it seems that it includes only Main? --- in which case I hope you can copy the other module's code from what you see below, and just give it the name that you see in Main).
Log results:
** Activity (main) Create, isFirst = true **
201: ggStr_Old2 =
202: Length of ggStr_Old2 = 0
203: ggStr_Old2 =
204: Length of ggStr_Old2 = 0
205: ggStr_Old2 = null
206: Length of ggStr_Old2 = 4
301: ggStr_Old3 =
302: Length of ggStr_Old3 = 0
303: ggStr_Old3 = null
304: Length of ggStr_Old3 = 4
305: ggStr_Old3 =
306: Length of ggStr_Old3 = 0
401: ggStr_Old4 =
402: Length of ggStr_Old4 = 0
403: ggStr_Old4 = null
404: Length of ggStr_Old4 = 4
405: ggStr_Old4 = 1234567
406: Length of ggStr_Old4 = 7
* Although all three subs (Sub2, 3, and 4) are similar in structure, please notice that each also has its own set of similarly defined variables. So they are not using the variables from another sub.
* Sub3 is virtually identical in structure to Sub2, EXCEPT for the addition of a .put statement that sets a value in the (supposedly empty/clear) map to "" (or null?). To see this notice that line 33 is commented out while line 57 is not. The question is, why does the addition of this statement affect the log entries that follow this statement, as compared to the corresponding log entries from Sub2? That is, if the map was already "cleared," why aren't log lines 303 and 304 identical to log lines 203 and 204. After all, the .put statement is supposed to return a null (not the string "null"), when I "put" a new key-value pair in an empty map, isn't it?
* Sub4 is similar to Sub3, but the two .put statements have been swapped. The logs for Sub4 are not surprising, at least not in light of Subs2 and 3. I included it for comparison purposes only.
Here is the code and the resulting log. The subroutine CallOtherSubs gets called from Main; and that's really all that Main does (e.g., no variables are dim'd in Main). The code is also attached (although it seems that it includes only Main? --- in which case I hope you can copy the other module's code from what you see below, and just give it the name that you see in Main).
B4X:
'Code module --- Subs in this code module will be accessible from all modules.
Sub Process_Globals
Dim ggMap2 As Map
Dim ggStr_Old2 As String
Dim ggStr_New2 As String
Dim ggMap3 As Map
Dim ggStr_Old3 As String
Dim ggStr_New3 As String
Dim ggMap4 As Map
Dim ggStr_Old4 As String
Dim ggStr_New4 As String
End Sub
Sub CallOtherSubs
Sub2
Sub3
Sub4
End Sub
Sub Sub2
ggMap2.Initialize
ggMap2.Clear
ggStr_Old2 = ""
ggStr_New2 = "1234567"
Log("201: ggStr_Old2 = " & ggStr_Old2)
Log("202: Length of ggStr_Old2 = " & ggStr_Old2.Length & CRLF & CRLF)
''' ggStr_Old2 = ggMap2.Put("Key1", "")
Log("203: ggStr_Old2 = " & ggStr_Old2)
Log("204: Length of ggStr_Old2 = " & ggStr_Old2.Length & CRLF & CRLF)
ggStr_Old2 = ggMap2.Put("Key1", ggStr_New2)
Log("205: ggStr_Old2 = " & ggStr_Old2)
Log("206: Length of ggStr_Old2 = " & ggStr_Old2.Length & CRLF & CRLF & CRLF & CRLF)
End Sub
Sub Sub3
ggMap3.Initialize
ggMap3.Clear
ggStr_Old3 = ""
ggStr_New3 = "1234567"
Log("301: ggStr_Old3 = " & ggStr_Old3)
Log("302: Length of ggStr_Old3 = " & ggStr_Old3.Length & CRLF & CRLF)
ggStr_Old3 = ggMap3.Put("Key1", "")
Log("303: ggStr_Old3 = " & ggStr_Old3)
Log("304: Length of ggStr_Old3 = " & ggStr_Old3.Length & CRLF & CRLF)
ggStr_Old3 = ggMap3.Put("Key1", ggStr_New3)
Log("305: ggStr_Old3 = " & ggStr_Old3)
Log("306: Length of ggStr_Old3 = " & ggStr_Old3.Length & CRLF & CRLF & CRLF & CRLF)
End Sub
Sub Sub4
ggMap4.Initialize
ggMap4.Clear
ggStr_Old4 = ""
ggStr_New4 = "1234567"
Log("401: ggStr_Old4 = " & ggStr_Old4)
Log("402: Length of ggStr_Old4 = " & ggStr_Old4.Length & CRLF & CRLF)
ggStr_Old4 = ggMap4.Put("Key1", ggStr_New4)
Log("403: ggStr_Old4 = " & ggStr_Old4)
Log("404: Length of ggStr_Old4 = " & ggStr_Old4.Length & CRLF & CRLF)
ggStr_Old4 = ggMap4.Put("Key1", "")
Log("405: ggStr_Old4 = " & ggStr_Old4)
Log("406: Length of ggStr_Old4 = " & ggStr_Old4.Length & CRLF & CRLF & CRLF & CRLF)
End Sub
Log results:
** Activity (main) Create, isFirst = true **
201: ggStr_Old2 =
202: Length of ggStr_Old2 = 0
203: ggStr_Old2 =
204: Length of ggStr_Old2 = 0
205: ggStr_Old2 = null
206: Length of ggStr_Old2 = 4
301: ggStr_Old3 =
302: Length of ggStr_Old3 = 0
303: ggStr_Old3 = null
304: Length of ggStr_Old3 = 4
305: ggStr_Old3 =
306: Length of ggStr_Old3 = 0
401: ggStr_Old4 =
402: Length of ggStr_Old4 = 0
403: ggStr_Old4 = null
404: Length of ggStr_Old4 = 4
405: ggStr_Old4 = 1234567
406: Length of ggStr_Old4 = 7