Extended field types returning null
BlitzMax Forums/BlitzMax Programming/Extended field types returning null
| ||
I cannot figure out why this is returning "Blah" as blank, when it is clearly set!SuperStrict Type Test Abstract Global List:TList = New TList Field Blah:String EndType Type MyTest Extends Test Field Blah:String = "Yo yo" End Type Test.List.AddLast(New MyTest) If Test.List.Count() <= 0 Then Print("Nothing in list!") Else For Local t:Test = EachIn Test.List Print "Blah = " + t.Blah Next EndIf |
| ||
Do it this way:SuperStrict Type Test Abstract Global List:TList = New TList Field Blah:String = "Yo yo" EndType Type MyTest Extends Test End Type Test.List.AddLast(New MyTest) If Test.List.Count() <= 0 Then Print("Nothing in list!") Else For Local t:Test = EachIn Test.List Print "Blah = " + t.Blah Next EndIf Or: SuperStrict Type Test Abstract Global List:TList = New TList Field Blah:String EndType Type MyTest Extends Test Method New() Self.Blah = "Yo yo" EndMethod End Type Test.List.AddLast(New MyTest) If Test.List.Count() <= 0 Then Print("Nothing in list!") Else For Local t:Test = EachIn Test.List Print "Blah = " + t.Blah Next EndIf Because MyTest had its own version of Blah it was messing it up... in other languages I'm pretty sure it wouldn't even compile. Last edited 2012 |
| ||
Well I want Blah to be "Yo yo" only in MyTest of course or I wouldn't have extended it, what if I had 4 or 5 Types extending Test, each with their own string in Blah? |
| ||
Well I managed to do a workaround by using methodes that sets Blah for each Type. It's quite ugly though (imo.) SuperStrict Type Test Abstract Global List:TList = New TList Field Blah:String Method SetBlah() Abstract EndType Type MyTest Extends Test Method SetBlah() Self.Blah = "My Test 1" End Method End Type Test.List.AddLast(New MyTest) Type MyTest2 Extends Test Method SetBlah() Self.Blah = "My Test 2" End Method End Type Test.List.AddLast(New MyTest2) If Test.List.Count() <= 0 Then Print("Nothing in list!") Else For Local t:Test = EachIn Test.List t.SetBlah() Print "Blah = " + t.Blah Next EndIf |
| ||
Then you would have to adjust "Method New()" for each of the different types (like done in the second "example"). In other languages like php you can do what you want (you create an custom field within your child - with the same name as in the parent) and are able to access it with eg. "super::myfieldname", if used with "$self->myfieldname" you access the local type-scope one.. But this is not available in blitzmax. bye Ron Your "SetBla"-Thingy is nothing more than the "Method New()"-Type already proposed - except it uses a "SetValue"-Naming-Pattern which may confuse. "SetXYZ" is normally for setting _private-fields or fields with special validation (ignore that sentence if you never will code in a team). the "New"-Method is called whenever a special type-instance is created - so every time you create one of the children. Last edited 2012 |
| ||
Yeah I'm using Method New() now, he didn't write that the first time, it was edited in after I had already done SetBlah() |