tags:

views:

230

answers:

3

What I am trying to do is test an object in ColdFusion to see what type of component it is. Something like...

<cfif isValid( "compath.dog", currentObj)>
    ...do something specific with dog objects...
</cfif>

I thought this was possible but receive an error saying the type I am passing does not correspond to one in the valid list of types...

Valid type arguments are: any, array, Boolean, date, numeric, query, string, struct, UUID, GUID, binary, integer, float, eurodate, time, creditcard, email, ssn, telephone, zipcode, url, regex, range , component, or variableName.

Is there a way to accomplish this in ColdFusion?

+5  A: 

You could use GetMetaData to find the type. Some quick code:

<cfif GetMetaData(currentObj).type eq "compath.dog">
Sam Farmer
It looks like GetMetaData may hold the solution. The "type" property just says "component" but there are other properties that do have the full inheritance path such as "name" and "fullname". However, I thought there was a way to test for relative types instead of fullpaths. I may just be remembering incorrectly though.
Dan Roberts
+2  A: 

you could use name or fullname from the getmetadata() function.

<cfif GetMetaData(currentObj).name eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

or

<cfif GetMetaData(currentObj).fullname eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

docs are here getmetadata() on what getmetadata() returns depending on the object type.

Jayson
+3  A: 

You could also use IsInstanceOf(). Though you must still use the full path, it can also be used to determine inheritance or identify components that implement a particular interface.

<cfif IsInstanceOf(obj, "compath.Dog")>
   yes. it is a dog component {woof}
<cfelse>
    some other type of component 
</cfif>

<cfif IsInstanceOf(obj, "compath.AnimalInterface")>
     yes. it implements the animal interface
<cfelse>
     no. it must be vegetable or mineral ...
</cfif>
Leigh
Thanks! This is exactly what I'm looking for. It does actually allow you to use a relative component path for type comparison which is what I wanted.
Dan Roberts