Typecasting replacement (sort of)

Blitz3D Forums/Blitz3D Programming/Typecasting replacement (sort of)

Mahan(Posted 2009) [#1]
Hi!

I wanted to share with you a method to handle instances of different types by their handle. This is useful if you got a handle of some type, but you don't know what particular type it is. (like if you got a list of handles of different types and you want to iterate the list and handle the different types individually depending of their type)

If "i" is your integer handle of an unknown type (of a given set of types) you can do this to handle each type individually:
	Select i
		Case Handle(Object.type1(i))
			Print "type1"
		Case Handle(Object.type2(i))
			Print "type2"
		Case Handle(Object.type3(i))
			Print "type3"
	End Select



This is particularly useful with my (free) collection userlib that includes List, StringList and StringMapList. With this library you can easily create several lists for a single type, but you have to store the handle for each instance in my lists and cast them to the correct type. Usually you store instances of a single type in a list. But should you choose to store instances of different types in the same list the method mentioned here might be of help. (the userlib works in BlitzPlus too.)


Below is a working demo of the method described:



(disclaimer: this is probably old news to the gurus i presume, but i couldn't find this particular method with forum-search)


Warner(Posted 2009) [#2]
That is a nice way. Thank you for sharing this!


Yasha(Posted 2009) [#3]
Huh. This solves a problem I was about to start thinking about. Thanks! Nice idea.


Mahan(Posted 2009) [#4]
Just a mini warning: This is not superfast (as you can see if you play around with the demo and change the searched type).

If you plan doing this on a few hundred/thousand handles per game loop there is probably no problem at all, but you can see how the time adds up in the demo when you do this millions of times. Otherwise it's completely good =)