map question

BlitzMax Forums/BlitzMax Beginners Area/map question

CS_TBL(Posted 2009) [#1]
It looks so simple in example code from around here, but I can't get this to work:
'SuperStrict

Local a:tmap=New tmap

MapInsert a,"yo!",123

Print String(MapValueForKey(a,"yo!"))


I get nothing printed, and in superstrict I get errors!


Brucey(Posted 2009) [#2]
A Map take only Objects... an Int is not an object.

You could store it as a String, or wrap it in an Object.


Czar Flavius(Posted 2009) [#3]
Here are the two ways! (Is it ok to mix strings and non-strings in the same map? I don't get that..)

Type TInt
	Function Create:TInt(pvalue:Int)
		Local temp:TInt = New TInt
		temp.value = pvalue
		Return temp
	End Function
	
	Field value:Int
End Type

Local map1:TMap = New TMap
map1.Insert("yo!", "123")

Local map2:TMap = New TMap
map2.Insert("hum", TInt.Create(123))

Print String(map1.ValueForKey("yo!"))
Print TInt(map2.ValueForKey("hum")).value



BladeRunner(Posted 2009) [#4]
No, mixing Objects and strings in a TMap will mess ist up as strings and ojs have different compare methods.


Brucey(Posted 2009) [#5]
Yes, you can mix the value part of a map entry with any types you like.
But for the key part, you should use all of the same types, otherwise it won't work very well.


CS_TBL(Posted 2009) [#6]
Righto.

It's easy enough for me to just store strings instead of ints. tnx.

May raise the question whether one could use real object ints/floats/etc. Not something extremely fancy, just something called OInt, OFloat etc. Like: local a:OInt=10, where a is a real object rather than an int..