Custom Types and TMaps
BlitzMax Forums/BlitzMax Beginners Area/Custom Types and TMaps
| ||
Is there a way to create a TMap with a values of a custom type? Example: Type TCountry Field Hemi$ Field People$ EndType Map=CreateMap() Local a:TCountry a.Hemi="West" a.People="American" MapInsert(Map,"america",a) Local b:TCountry b.Hemi="East" b.People="Chinese" MapInsert(Map,"china",b) Local c:TCountry c.Hemi="West" c.People="Canadian" MapInsert(Map,"canada",c) Local d:TCountry d.Hemi="East" d.People="Indian" MapInsert(Map,"india",d) Repeat In$=Input$() If MapContains(Map,In) Print "Hemisphere: "+MapValueForKey(Map,In).Hemi Print "People: "+MapValueForKey(Map,In).People Else Print "Not Recognised" EndIf Until In="" |
| ||
WOW! I searched and searched...gave up...created a topic...wandered around...found the solution ten minutes later. Apparently you have to cast the value to the type. Print "Hemisphere: "+TCountry(MapValueForKey(Map,In)).Hemi Print "People: "+TCountry(MapValueForKey(Map,In)).People |
| ||
Sure, all datastructures only return OBJECT, if you want to access anything on it, you first must have an object of your desired class again. |
| ||
This gave me some grief at one point, too, until I got the hang of doing a type cast. One of the many things in BlitzMax that needs better documentation. Mark could probably give himself a fairly big pay rise by giving the docs some serious love. Most people are put off from buying products with inadequate documentation. |
| ||
Just to be nosey, might you consider making the hemisphere and possibly the people into types of their own? Storing the text string east/west/etc multiple times doesn't seem an efficient solution and is easy to make inconsistant. |