map question
BlitzMax Forums/BlitzMax Beginners Area/map question
| ||
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! |
| ||
A Map take only Objects... an Int is not an object. You could store it as a String, or wrap it in an Object. |
| ||
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 |
| ||
No, mixing Objects and strings in a TMap will mess ist up as strings and ojs have different compare methods. |
| ||
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. |
| ||
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.. |