InstanceOf()

BlitzMax Forums/BlitzMax Programming/InstanceOf()

How can I tell if an object is an instance of a specific type, and call type specific methods in it?

Closest you'll get... possibly...
Instanceof

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


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.

thanks alot - this really helps :D

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.

Of course this will give "false" positives if inheritance is involved.
So will InstanceOf. :o>