Map.ContainsKey does not cast integer

danvica

Member
Licensed User
Longtime User
This code doesn't work:

B4X:
dim m as map
dim i,Val as int

m=File.ReadMap(File.DirInternal,"myfile.ini")

Val=5

for i=0 to m.size-1
  if m.ContainsKey(Val)=false then '(Always return false)
  ....
  end if
next

This works:

B4X:
dim m as map
dim i,Val as int
dim TempVal as string

m=File.ReadMap(File.DirInternal,"myfile.ini")

Val=5

TempVal=Val

for i=0 to m.size-1
  if m.ContainsKey(TempVal)=false then 'Works correctly 
  ....
  end if
next

I'm still a newbie and maybe I shouldn't call it bug... if you have other explanation please let me know.
 

magalt

Member
Licensed User
Longtime User
It is not a bug.
The documentation of the ContainsKey (member of Map) say:

ContainsKey (Key As Object) As Boolean
Tests whether there is an item with the given key.
Example:
If Map.ContainsKey("some key") Then ...

Marco
 

agraham

Expert
Licensed User
Longtime User
Map.ContainsKey is not expected to do any type casting. A Map can hold any type of object as both keys and values so you must give it the correct type of object for it to work as expected. In your case ReadMap populates the Map with Strings as both keys and values so you must pass a String type to ContainsKey.
 

danvica

Member
Licensed User
Longtime User
Ok. I still have to better understand when the compiler decides to cast something. I'm used to C for microcontroller that gives you no help at all...

Next times it's better to use discussion forum...:sign0089:

Thanks for you patience...
 
Top