Android Question Map Help

paris7162

Member
Licensed User
Longtime User
When using variables with Maps I always return null value. What am I doing wrong here?


B4X:
Sub Populate_Tabs
Dim Alpha As String
Dim k As Int
Dim Map1 As Map
Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Alpha = Alpha.ToLowerCase

Map1.Initialize


For i = 0 To 3
    For h = 0 To 3
    p(i,h) = Alpha.CharAt(k)
    Map1.Put(Alpha.CharAt(k),p(i,h))
    k = k + 1
    'BoggleLetters.Add(p(i,h))
    Next
Next

Alpha = Map1.Get("c")
Alpha = Map1.Get("C")

End Sub
 

paris7162

Member
Licensed User
Longtime User
I intialized k to 0. in my Activity_Create function. But I added it to the code here.

B4X:
Sub Populate_Tabs
Dim Alpha As String
Dim k As Int
Dim Map1 As Map
Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Alpha = Alpha.ToLowerCase
k = 0
Map1.Initialize


For i = 0 To 3
    For h = 0 To 3
    p(i,h) = Alpha.CharAt(k)
    Map1.Put(Alpha.CharAt(k),p(i,h))
    k = k + 1
    'BoggleLetters.Add(p(i,h))
    Next
Next

Alpha = Map1.Get("c")
Alpha = Map1.Get("C")

End Sub
 
Upvote 0

paris7162

Member
Licensed User
Longtime User
You set all characters to lower case !
The key and the values in Map1 are the same.
Alpha = Map1.Get("C") will give a Null value because there is no "C" key !


I only added the Capital "C" just to show that I used both cases. The first Map.Get was for lowercase "c". In most cases the map won't be same; however, sometimes they will. In theory, I should be able to get the map no matter what the key is, right?
 
Upvote 0

paris7162

Member
Licensed User
Longtime User
As it turn out, if I trim the key before I put it into the Map it words perfectly. Thanks for all of the help.

B4X:
Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Alpha = Alpha.ToLowerCase
k = 0
Map1.Initialize


For i = 0 To 3
    For h = 0 To 3
        xtest = Alpha.CharAt(k)
        xtest = xtest.Trim
        Map1.Put(xtest,Alpha.CharAt(k))
        k = k + 1
    Next
Next

Log(Map1.Get("c"))
 
Upvote 0
Top