Other MAPS QUESTION

MarcoRome

Expert
Licensed User
Longtime User
Hi, All, @Erel
i have this code ( as example )

B4X:
    Dim makemap As Map

    makemap.Initialize
    For i = 0 To 5
        makemap.Put($"${i}"$,$"A-${i}"$)
        'makemap.Put(i,$"A-${i}"$)
    Next
    Log(makemap)
  
    Log(makemap.GetKeyAt(2))
    Log(makemap.GetValueAt(2))

    Log(makemap.Get(2))
  
    Log("******************")

if I run the first
makemap.Put($"${i}"$,$"A-${i}"$)
I have the following result:
Waiting for debugger to connect...
Program started.
(MyMap) {0=A-0, 1=A-1, 2=A-2, 3=A-3, 4=A-4, 5=A-5}
2
A-2
null
******************


if I run the second
makemap.Put(i,$"A-${i}"$)
I have the following result
Waiting for debugger to connect...
Program started.
(MyMap) {0=A-0, 1=A-1, 2=A-2, 3=A-3, 4=A-4, 5=A-5}
2
A-2
A-2
******************

Any Bug or my wrong ??
 
Last edited:

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
It looks like a type issue,

In the first case the type of the key is a string and so does not match an int in the get. "2" vs 2. In some cases there maybe implicit type conversion.

In the second case, the key is an int and matches the int in the get, 2 vs 2.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
It looks like a type issue,

In the first case the type of the key is a string and so does not match an int in the get. "2" vs 2. In some cases there maybe implicit type conversion.

In the second case, the key is an int and matches the int in the get, 2 vs 2.
Yes is so.
If is type String it does not cast.
Only in this way work
B4X:
    Dim makemap As Map

    makemap.Initialize
    For i = 0 To 5
        'makemap.Put($"${i}"$,$"A-${i}"$)
        makemap.Put(i,$"A-${i}"$)
    Next
    Log(makemap)
 
    Log(makemap.GetKeyAt(2))
    Log(makemap.GetValueAt(2))

    Log(makemap.Get(2))
 
    For Each Key As Int In makemap.Keys  '<---- AS INT 
        Dim value As String = makemap.Get(Key)
        Log($"Key: ${Key} Value:${value}"$)
    Next

Return:

Waiting for debugger to connect...
Program started.
(MyMap) {0=A-0, 1=A-1, 2=A-2, 3=A-3, 4=A-4, 5=A-5}
2
A-2
A-2
Key: 0 Value:A-0
Key: 1 Value:A-1
Key: 2 Value:A-2
Key: 3 Value:A-3
Key: 4 Value:A-4
Key: 5 Value:A-5
******************

In second Case
B4X:
    Dim makemap As Map

    makemap.Initialize
    For i = 0 To 5
        makemap.Put($"${i}"$,$"A-${i}"$)
        'makemap.Put(i,$"A-${i}"$)
    Next
    Log(makemap)
 
    Log(makemap.GetKeyAt(2))
    Log(makemap.GetValueAt(2))

    Log(makemap.Get(2))
 
    For Each Key As String In makemap.Keys '<---- AS STRING
        Dim value As String = makemap.Get(Key)
        Log($"Key: ${Key} Value:${value}"$)
    Next

Return:

Waiting for debugger to connect...
Program started.
(MyMap) {0=A-0, 1=A-1, 2=A-2, 3=A-3, 4=A-4, 5=A-5}
2
A-2
null
Key: 0 Value:A-0
Key: 1 Value:A-1
Key: 2 Value:A-2
Key: 3 Value:A-3
Key: 4 Value:A-4
Key: 5 Value:A-5
******************
 
Upvote 0

pliroforikos

Active Member
Licensed User
I think the first one map is
"0" = "A-0"
"1" = "A-1"
...
Notice that the " " are not shown but the key and value are strings
So you have to Log(Log(makemap.Get("2")) <-- 2 as string

In second map is

0 = "A-0"
1 = "A-1"
...
So you have to log(makemap.Get(2)) <-- 2 as int
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
I think the first one map is
"0" = "A-0"
"1" = "A-1"
...
Notice that the " " are not shown but the key and value are strings
So you have to Log(Log(makemap.Get("2")) <-- 2 as string

In second map is

0 = "A-0"
1 = "A-1"
...
So you have to log(makemap.Get(2)) <-- 2 as int
Right also this
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Type Object:
B4X:
    For Each Key In makemap.Keys '<---- OBJECT
        Dim value As String = makemap.Get(Key)
        Log($"Key: ${Key} Value:${value}"$)
    Next
1626544979393.png
1626545027815.png


Case Sensitive:
B4X:
    Dim makemap As Map

    makemap.Initialize
    For i = 0 To 5
'        makemap.Put($"${i}"$,$"A-${i}"$)
        makemap.Put(i,$"A-${i}"$)
    Next
    Log(makemap)

     Dim i As Int = 2
    Log(makemap.GetKeyAt(i))
    Log(makemap.GetValueAt(i))

Read Note
1626544860585.png


See: GetKeyAt and GetValueAt deprecated

1626545470186.png
 
Last edited:
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
OK. Ultimately if you treat them as an object everything works. Thinking about it, it is also right that this is the case, given that you can enter any type of data in a map.

B4X:
Dim makemap As Map

    makemap.Initialize
    For i = 0 To 5
        makemap.Put($"${i}"$,$"A-${i}"$)
        'makemap.Put(i,$"A-${i}"$)
    Next
    
    For Each Key In makemap.Keys
        Dim value As String = makemap.Get(Key)
        Log($"Key: ${Key} Value:${value}"$)
    Next
 
Upvote 0
Top