Type fields

BlitzMax Forums/BlitzMax Beginners Area/Type fields

After Blitz has created the compiled code it doesnt know what the field names are anymore. One solution is to not use fields at all and create your own 'variable' type held in a list, with Getters and Setters. Something like this:

Strict

Type variable
	Field name$
	Field value$
	
	Global _list:TList

	Method New()
		If variable._list = Null
			variable._list = New TList
		EndIf
		variable._list.addlast(Self)
	EndMethod
	

	Function init()	
		If variable._list = Null
			variable._list = New TList
		EndIf
	End Function
	
	Function Set(name$,value$)
		For Local v:variable = EachIn variable._list
			If v.name = name
				v.value = value
				Return True
			EndIf
		Next
		Local v:variable = New variable
		v.name = name
		v.value = value
		Return False
	End Function	
	
	Function Get$(name$)
		For Local v:variable = EachIn variable._list
			If v.name = name
				Return v.value
			EndIf
		Next
		Return "[undefined]"
	End Function		

End Type



variable.init

For Local f = 0 To 20
	variable.Set "myvar_"+f,f+20
Next

Print variable.Get("myvar_10")


For Local v:variable = EachIn variable._list
	Print v.value
Next

Print variable.Get("myvar_50")

You can speed things up using a binary tree.

OR you can use the built in one in Bmax:
Strict

Import BRL.Map


Local map:TMap = New TMap

map.Insert("blah","20")
Print String(map.ValueForKey("blah"))