object VS unknown type
BlitzMax Forums/BlitzMax Programming/object VS unknown type
| ||
Damn, I'm about to explode: what's wrong with the following code? : In my module: Function Append(t:Object[] Var,o:Object) Local size:Int = Len(t) t = t[..size+1] t[size] = o End Function In my TestMyModuleFile: Type MyType Field s:String End Type Global t:MyType[] Global m:MyType Print "Append firsttry:"+Chr(13) m = New MyType m.s = "firsttry" t = Append(t,m) I just tried making a general Append procedure that receive an array that can be modified (var) and add it something of the same type... The compiler compiles my mod, no prob here, but my test returns an error like: "Unable to convert from MyType Array to Object Array Var" How could I make this fu..... function that is REALLY beginning to piss me off????? (KamaShin on a rampage, ready to kill everything that moves) |
| ||
You can remove the Var specifier from the Append function since objects and arrays are always passed by reference. |
| ||
hmmm yep I ve tested that but then the Append function wouldn't do what it's supposed to (though indeed without the "var" everything compiles fine)... the array though doesn't seem to be modified the slightest... |
| ||
Hi, That really should work. I'll sort this out soon, but in the meantime you could just pass the modified array back, eg: Function Append:Object[]( t:Object[],o:Object ) Local size:Int = Len(t) t=t[..size+1] t[size] = o Return t End Function Type MyType Field s:String End Type Global t:MyType[] Global m:MyType Print "Append firsttry:"+Chr(13) m = New MyType m.s = "firsttry" t = MyType[]( Append(t,m) ) 'have to convert returned Object[] to MyType[]. Print t[0].s |
| ||
YEAAAAAYYY... it works... It's a bit annoying to have to write MyType[]( OneOfTheFunctions) but at least it work... I ll use that while waiting for you to fix the fact that an array isn't modified when passed as argument... Thanks a lot fo the answer :) |