Testing for existence of a property

178 views
Skip to first unread message

Marc Lang

unread,
Oct 3, 2012, 6:20:36 AM10/3/12
to InterSystems: Ensemble in Healthcare
I have some recursive code that parses a file and sets values of properties in a class based on those values.
It's fairly flexible and dynamic though, so the class it's writing to may not always have properties to hold all the values.

How do I test for the existence of a property in a class before setting it?
If I try set a property that doesn't exist I get <PROPERTY DOES NOT EXIST>

I have set up a Catch to catch that error and swallow it, but hoping for a more elegant solution?

Try {

// First check the property exists
set help = $property(pObject,propname) // THROWS ERROR HERE IF PROPERTY DOESN'T EXIST

// doing stuff
Set $property(pObject,propname) = value


}
Catch ex {

If (ex.Name = "<PROPERTY DOES NOT EXIST>")
{
// Do nothing
}

}

Andrew Makinson

unread,
Oct 3, 2012, 9:15:16 AM10/3/12
to ensemble-in...@googlegroups.com
Does $Data command work on properties? Not near a cache server so can't test.

I.e 

For a variable we can use 

IF $D(x) write "exists"


Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en

Lexi Hayden

unread,
Oct 3, 2012, 10:46:25 AM10/3/12
to Ensemble-in...@googlegroups.com, ensemble-in...@googlegroups.com
You could probably use %Dictionary.CompiledClass to see if the class definition includes the property.
To unsubscribe from this group, send email to Ensemble-in-Healthcare-unsub...@googlegroups.com

Dale du Preez

unread,
Oct 3, 2012, 10:54:34 AM10/3/12
to ensemble-in...@googlegroups.com
In the same vein, you could use the %Dictionary.CompiledProperty class as follows to do the exact lookup you want:

    If ##class(%Dictionary.CompiledProperty).IDKEYExists($classname(pObject),propname) {
        Set $property(pObject,propname) = value
    }

If you are on a much older version of Ensemble/Cache where $classname() is not available, you can use pObject.%ClassName(1), but this not as fast as $classname(pObject).

Note that the approach above may have more overhead than simply using a try/catch block as you need to do a database lookup.

Dale
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com

Ted Peck

unread,
Oct 3, 2012, 10:54:59 AM10/3/12
to ensemble-in...@googlegroups.com, Lexi Hayden
Another good trick is to put a try/catch around the property access. If it doesn't exist you will catch a <PROPERTY DOES NOT EXIST> exception. This is actually more efficient in most cases than querying the class dictionary.


On 10/3/2012 10:46 AM, Lexi Hayden wrote:
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com

Ted Peck

unread,
Oct 3, 2012, 10:59:09 AM10/3/12
to ensemble-in...@googlegroups.com, Lexi Hayden
Whoops - I just noticed that's where this thread started. Still, it's not as ugly as it seems.

Marc Lang

unread,
Oct 4, 2012, 8:07:01 AM10/4/12
to ensemble-in...@googlegroups.com
Thanks folks.

I'll create a utility function based on the try/catch method, which accepts an object and property name., and returns true/false.
That hides the guts of it behind a one-line function call, and should be pretty fast.

Thanks
Reply all
Reply to author
Forward
0 new messages