B4J Question Int<>Long?

Vikjh

Member
Licensed User
I have JRDC-Like Project thet return me a K-Val data (listOfMaps) from SQL database for fill my CustumControl (controlsfx SearchableComboBox)
The problem is with SetSelectingID and "KValMap" thhet has Key as Object

IF the database return key=10 "as long" and I try to "SetSelectedID" "as Int" nothing happend, if same "key as Long" all ok!

Code: Copy
Private Sub Class_Globals
   Private KValMap as Map
   Private ComboboxJO as JavaObject
end sub

Public Sub Fill_CB(ListOFMaps as List)
    KValMap.Initialize
    Dim Items as List=ComboboxJO.RunMethod("getItems",Null)
    For Each ItemMap As Map In ListOFMaps
        Items.Add(ItemMap.GetValueAt(1))
        KValMap.Put(ItemMap.GetValueAt(0),ItemMap.GetValueAt(1))
    Next
end sub

Public Sub SetSelectedID(ID As Object)
    If KValMap.ContainsKey(ID) Then SetSelectedItem(KValMap.Get(ID))
End Sub

Public Sub SetSelectedItem(Item As Object)
    ComboboxJO.RunMethodjo("getSelectionModel",Null).RunMethod("select",Array(Item))
End Sub

In my case "ID", can be: String, Long, Int, Float (Not in the same time!), and I Use "Object" for it

I'm try small example
B4X: Copy
   Dim Longkey As Long =10
    Dim IntKey As Int =10
    Dim m As Map=CreateMap(10:0)

    If m.ContainsKey(Longkey) Then LogDebug("Longkey")
    If m.ContainsKey(IntKey) Then LogDebug("IntKey")

And result only "IntKey"
10 "as Int" <> 10 "as Long"?


Sorry for my english :)
 
I think CreateMap() casts the keys and values to appropriate data type automatically.

Consider this:
B4X: Copy
Dim m As Map = CreateMap(10: 20, 300000000000000: 400000000000000, 50: 600000000000000)
For Each Key As Object In m.Keys
    Log(GetType(Key) & " : " & GetType(m.Get(Key)))
Next
' java.lang.Integer : java.lang.Integer
' java.lang.Long : java.lang.Long
' java.lang.Integer : java.lang.Long

If you want it to be Long then pass the value to a Long variable then to the CreateMap.
B4X: Copy
Dim Longkey As Long = 10
Dim m As Map = CreateMap(Longkey: 0)
For Each Key As Object In m.Keys
    Log(GetType(Key) & " : " & GetType(m.Get(Key)))
Next
Log(m.ContainsKey(Longkey))
' java.lang.Long : java.lang.Integer
' true

Note: GetKeyAt and GetValueAt are deprecated.

Usually we use String for column names in database so I think the keys should be in String.
 
Upvote 0
When using Maps, the object type of the key is important and needs to be the same as the comparison value.

In this case, 10 As Int is not equal to 10 As Long.

To test this further try:

B4X: Copy
If m.ContainsKey(Longkey.As(Int)) Then LogDebug("key found")
 
Upvote 0
Upvote 0
Thanks for the response. I will use a "String" for the keys, it seems more reliable than an "Оbject"
10 "as String" always = 10 "as String" :)
 
Upvote 0
Back
Top