determining an objects type?

BlitzMax Forums/BlitzMax Beginners Area/determining an objects type?

peltazoid(Posted 2005) [#1]
HI, i'm sure I read something, even produced an example for myself on this. but I can not seem to find it at present.

the problem is this, determining if an object belongs to a particular type

for example (this is not leagal code)
strict

type foo
  field x
end type

local x : foo = new foo

if x = foo() then print "x is a foo"
end

can anyone enlighten me on how to do this correctly.

cheers.


Who was John Galt?(Posted 2005) [#2]
if foo(x) print "x is a foo"
...works I think.


kyoryu(Posted 2005) [#3]
if foo(x) <> null
...
endif

definitely works. Casts return null if they can't cast to the type requested.

Only thing to note, though, is that if foo is a base class of bar, then that check will also return a positive for any bar objects. If that's confusing, check this example.

Type Foo
   Field x:Int
End Type

Type Bar Extends Foo
   Field y:Int
End Type

Local mybar:Bar = new Bar()
if( foo(mybar) <> null )
   print "mybar is a foo."
endif



degac(Posted 2005) [#4]
try this

Local mybar:Bar = new Bar '<--- without brackets!!!

it should work now


Who was John Galt?(Posted 2005) [#5]
Yeah... if variable is just short for 'if variable<>0' or if variable<>null' etc depending on the variable's type.


peltazoid(Posted 2005) [#6]
thanks, works a treat.

Thing is I have used the notation in a couple of test examples when I first got max! I just can not have save the examples or remeber where I saw the comparison been done.

thanks again.