'Default' Object

BlitzMax Forums/BlitzMax Beginners Area/'Default' Object

KrayzBlu(Posted 2006) [#1]
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 :)


Diablo(Posted 2006) [#2]
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



KrayzBlu(Posted 2006) [#3]
OK, but how would I get that function to return the same kind of Object/Type ?


Diablo(Posted 2006) [#4]
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


KrayzBlu(Posted 2006) [#5]
Yeah that doesn't seem to work :/


Diablo(Posted 2006) [#6]
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


KrayzBlu(Posted 2006) [#7]
No, it gives me 'Unable to convert from 'Int' to Object' :/


Diablo(Posted 2006) [#8]
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.


Dreamora(Posted 2006) [#9]
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.


KrayzBlu(Posted 2006) [#10]
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)


KrayzBlu(Posted 2006) [#11]
Aha OK I got it, thanks for your help :)


KrayzBlu(Posted 2006) [#12]
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....