Hi, I'm trying to make a function that acts like a list, but basically just can take any kind of object (type), operate on it, and output that same type of object/type without actually knowing (or caring) what kind of object/type it is. Is there any easy way to do this?
Thanks,
KrayzBlu
Edit:
That's assuming of course that whatever object/type is inputted carries the same standard fields/methods :)
you are going to have to look into type casting which is done by:
[typename] ( [objectname] )
i.e.
Type TBase
Field index%
End Type
Type TEx Extends TBase
Field name$
End Type
Global thing:TEx = New TEx
thing.index = 1
thing.name = "bob"
Print thing.index
Print thing.name
Increase(thing)
Print thing.index
Print thing.name
Function Increase(_object:Object)
TBase(_object).index:+ 1
End Function
OK, but how would I get that function to return the same kind of Object/Type ?
As far as I know you cant but you can just go:
function MyFunc:Object(_object:object)
end function
And then type cast the result.
I'm probaly wrong tho ;P
Yeah that doesn't seem to work :/
mmm... Are you type casting the result? like:
Type TBase
Field index%
End Type
Type TEx Extends TBase
Field name$
End Type
Global thing:TEx = New TEx
thing.index = 1
thing.name = "bob"
Print thing.index
Print thing.name
thing2:TEx = TEx(Increase(thing))
Print thing2.index
Print thing2.name
Function Increase:Object(_object:Object)
TBase(_object).index:+ 1
Return _object
End Function
And are you sure your type casting to the correct object?
Other then that - some one else might be able to help . :S
No, it gives me 'Unable to convert from 'Int' to Object' :/
Oo wired. I never got that error and I ran it in both debug and not.
Output is:
1
bob
2
bob
This is running in blitzmax version 1.18.
You can't convert numerics to anything beside other numerics as numerics are no objects.
You need to transfer numerics as strings, strings are objects.
Hmm, I got it to work, but now when I try to turn it inot an object array, it dosnt seem to be returning the right thing (it returns an array of length 0)
Aha OK I got it, thanks for your help :)
Wait.... Nver Mind :(
Slices seem to 'reset' the object array for some reason
eg with:
Type TBase
Field index%
End Type
Type TEx Extends TBase
Field name$
End Type
Global thingarray:TEx[2]
Global thing:TEx = New TEx
Print Len thingarray
thingarray = TEx[](Increase(thingarray))
Print Len thingarray
Using this function returns 2 and 2
Function Increase:Object(_object:Object[])
Return _object
End Function
But using this function returns 2 and 0
Function Increase:Object(_object:Object[])
_object = _object[..4]
Return _object
End Function
Any Ideas?
Thanks....