Bug? Integer type casting and maps

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I have various items in a map some of them are ints.

I have found that if you compare 2 items as follows the results do not always work as expected.
Each of the following maps tm and chktm have a key value pair with a name of idTeam and a value of type int.

B4X:
   if (tm.get("idTeam") = chktm.get("idTeam")) then
      Log("Match found") ' Match is sometimes found but not always
    end if

bit this always works

B4X:
   private v1,v2 as int
   v1 = tm.get("idTeam")
   v2 = chktm.get("idTeam")
   if (v1 = v2) then
      Log("Match found") ' This always works as expected
    end if

Is this expected behavior with maps?
I'm having to go through all my code and update comparisons.

Thanks
Andrew
 

DonManfred

Expert
Licensed User
Longtime User
Is this expected behavior with maps?
It sompares one OBJECT with another OBJECT (the map does contains Objects.)
It is expected i would say.
In case
B4X:
tm.get("idTeam") = chktm.get("idTeam")
does work then the values inside the tm Object and the chktm Object (the value inside) are the same objects you added...
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Fair enough, if it is the expected behavior.

It would be great if you could write

B4X:
(int)tm.get("idTeam") = (int)chktm.get("idTeam")

or some such to force the casting rather than doing the assignments, but now I know, I will code accordingly.

Cheers

Andrew
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
sub mycheckingsub(in as Map) As Int
dim value as int = in.get("idTeam")
return value
endsub
you can write a small helper sub which cast a Object to a int and return the int....
B4X:
mycheckingsub(tm.get("idTeam")) = mycheckingsub(chktm.get("idTeam"))
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Good idea! that feels a bit neater

wouldn't the code be

B4X:
sub mycheckingsub(in as Object) As Int
dim value as int = in
return value
end sub
 

DonManfred

Expert
Licensed User
Longtime User
wouldn't the code be
then you must send the value from the Object inside the call. Does work too. I suggest to use the map and also give the value you want to return from the map.

Depends on your needs/what is more confortable for you...
 
Top