InstanceOf()

BlitzMax Forums/BlitzMax Programming/InstanceOf()

Will(Posted 2005) [#1]
How can I tell if an object is an instance of a specific type, and call type specific methods in it?


tonyg(Posted 2005) [#2]
Closest you'll get... possibly...
Instanceof


Koriolis(Posted 2005) [#3]
You just cast it, if thee cast succeded, then it's of the required type:
Local a:MyTypeA = ...
Local b:MyTypeB = MyTypeB(a)
If b <> Null Then
    'OK, right type
Else
    ' Wrong type, the object that 'a' points to isn't a 'MyTypeB'
EndIf



Gabriel(Posted 2005) [#4]
Or just

If MyTypeB(a)
   ' IT'S A B
else
   ' IT'S NOT A B
end if


Of course this will give "false" positives if inheritance is involved. I say "false" because they're not really false, but they might not be what you want. If type b is an extended class based on the base type a, then it will show as being an a ( which it is ) but it just depends if this bothers you. With good coding practices, it probably won't. I can't imagine you'd want to differentiate in this way.


Will(Posted 2005) [#5]
thanks alot - this really helps :D


Koriolis(Posted 2005) [#6]
Or just ...
Sure, except if you want to
tell if an object is an instance of a specific type, and call type specific methods in it
you'll need the cast object anyway.


FlameDuck(Posted 2005) [#7]
Of course this will give "false" positives if inheritance is involved.
So will InstanceOf. :o>