Android Question Use a char as the key for a map

Alessandro71

Well-Known Member
Licensed User
Longtime User
Something I wasn't able to find: how to declare a char in the IDE that is not CHR(xx)
"A" is really string, not a single char, and I need a map as the following:
B4X:
Dim countrymap As Map = CreateMap( _
    "1": "USA", _
    "3": "Mexico", _
    "5": "USA", _
    "J": "Japan", _
    "S": "UK" _
)
How can I specify the key as a single char?
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Technically, "1" is also a char as well as a string so I'm not sure what you are trying to accomplish. But, to answer your question:
B4X:
Dim countrymap As Map = CreateMap( _
         Chr(Asc("1")): "USA", _
        Chr(Asc("3")): "Mexico", _
        Chr(Asc("5")): "USA", _
        Chr(Asc("J")): "Japan", _
        Chr(Asc("S")): "UK" _
        )

For context:
B4X:
    Dim pcChar As Char = Asc("A")
    Dim psString As String = "A"
    
    If pcChar = psString Then
        Log("Char = String")
    Else
        Log("Char <> String")
    End If
    If pcChar = "A" Then
        Log("Char = 'A'")
    Else
        Log("Char <> 'A'")
    End If
    If pcChar = 65 Then
        Log("Char = 65")
    Else
        Log("Char <> 65")
    End If
Log output:
B4X:
** Activity (main) Create, isFirst = true **
Char <> String
Char <> 'A'
Char = 65
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
thank you for your answer
to add a little more context
B4X:
countrymap.Get(m_VIN.CharAt(0))
won't work with my first declaration, while it works with yours
i was wondering if there's a more compact way to specify a single char instead of chr(asc(x))
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What you don't say is why you want to use a Char as a key for the map.
EDIT: sorry you did.

If you are getting the keys from somewhere else to query the map, it may be easier to use strings and convert the key to a string on accessing the map, something like:

B4X:
M.Get(Key & "")
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Where is m_VIN defined? You could use a string array, or list or even substring instead of extracting a char
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
If you are getting the keys from somewhere else to query the map, it may be easier to use strings and convert the key to a string on accessing the map, something like:

B4X:
M.Get(Key & "")

thanks for your clever way to convert a char to a string on the fly: i missed it
but i still prefer to avoid conversions and work only with the minimal data required
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
i think extracting a single char is faster than a substr

Is speed an issue over simplicity.

If you are accessing the map key via an index, do you really need to use a map at all? you could access a list directly via the index.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
If you are accessing the map key via an index, do you really need to use a map at all? you could access a list directly via the index.

code maintainability: a map declared in the global sections has better readability than a runtime built list
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top