Field is zero! (Reflection)

BlitzMax Forums/BlitzMax Programming/Field is zero! (Reflection)

Hezkore(Posted 2011) [#1]
I'm trying to use reflection to build my system, but whenever I try to retrieve a value from a field, it comes back as zero! :o
Type Test
	Field Test:Int = 5
EndType

Local id:TTypeId = TTypeId.ForName("Test")
For Local fld:TField = EachIn id.EnumFields()
	Print "Field: " + fld.Name() + " = " + fld.GetInt(Null)
Next


By my logic, that should say "Field: Test = 5" but instead it's "= 0", WHY?! D:


degac(Posted 2011) [#2]
Well, If I'm not wrong there is any object of type 'Test', so no field with value '5'


Type Test
	Field myfield:Int = 5
	Field two:String="This is a string 'two'"
EndType

Local id:TTypeId = TTypeId.ForName("Test")

Local myobj:Test=New Test


For Local fld:TField = EachIn id.EnumFields()



	Print "Field: <" + fld.name() + "> <" +fld.TypeId().Name()+"> = "+fld.GetString(myobj)
	
	
Next



Last edited 2011


Hezkore(Posted 2011) [#3]
Yeah, I was worried that'd be the issue...
Isn't there a way around this without creating an instance of the type?
I noticed that it works if you first use fld.SetInt(Null,5) it'll work just fine!


degac(Posted 2011) [#4]

I noticed that it works if you first use fld.SetInt(Null,5) it'll work just fine!


Well, to me it seems like a bug: as there's no object, no result should be available or changeable


col(Posted 2011) [#5]
Hiya guys. It's not a bug. It's working correct in the same that you can set a default value in the type definition but instead you can use reflection to do the same thing.