Hello
My first wonder:
B4X language guid states about map collections that "The key should be a string or a number". Why is that true? I've been using objects as key a lot and for me it seems to work without problems!
My other wonder:
I have a problem where I want to use 2 numbers as a key in a map. I came up with a solution where I use smartstrings to create the key. Look at the code and log output below to see what I mean.
What do you think about doing it that way? Does my code provide effective lookups in the map collection?
My first wonder:
B4X language guid states about map collections that "The key should be a string or a number". Why is that true? I've been using objects as key a lot and for me it seems to work without problems!
My other wonder:
I have a problem where I want to use 2 numbers as a key in a map. I came up with a solution where I use smartstrings to create the key. Look at the code and log output below to see what I mean.
What do you think about doing it that way? Does my code provide effective lookups in the map collection?
B4J Non-UI application:
Sub Process_Globals
Type keyStruct (firstKey As Int, secondKey As Long)
End Sub
Sub AppStart (Args() As String)
Dim key1 As keyStruct = CreatekeyStruct(1,50)
Dim key2 As keyStruct = CreatekeyStruct(1,51)
Dim key3 As keyStruct = CreatekeyStruct(2,50)
Dim otherKey3 As keyStruct = CreatekeyStruct(2,50)
Dim MapWithSmartstringKeys As Map = CreateMap( _
$"${key1}"$:"value 1", _
$"${key2}"$:"value 2", _
$"${key3}"$:"value 3")
Dim MapWithRegularUseOfKeys As Map = CreateMap( _
key1:"value 1", _
key2:"value 2", _
key3:"value 3")
Log("-- MapWithSmartstringKeys --")
Log(MapWithSmartstringKeys.Getdefault($"${key1}"$,"key1 didn't look up av value"))
Log(MapWithSmartstringKeys.Getdefault($"${CreatekeyStruct(1,51)}"$,"CreatekeyStruct(1,51) didn't look up av value"))
Log(MapWithSmartstringKeys.Getdefault($"${otherKey3}"$,"otherKey3 didn't look up av value"))
Log(" ")
Log("-- MapWithRegularUseOfKeys --")
Log(MapWithRegularUseOfKeys.Getdefault(key1,"key1 didn't look up av value"))
Log(MapWithRegularUseOfKeys.Getdefault(CreatekeyStruct(1,51),"CreatekeyStruct(1,51) didn't look up av value"))
Log(MapWithRegularUseOfKeys.Getdefault(otherKey3,"otherKey3 didn't look up av value"))
End Sub
Public Sub CreatekeyStruct (firstKey As Int, secondKey As Long) As keyStruct
Dim t1 As keyStruct
t1.Initialize
t1.firstKey = firstKey
t1.secondKey = secondKey
Return t1
End Sub
Log output:
-- MapWithSmartstringKeys --
value 1
value 2
value 3
-- MapWithRegularUseOfKeys --
value 1
CreatekeyStruct(1,51) didn't look up av value
otherKey3 didn't look up av value