Question
Flávio Lúcio Na... · Jun 22, 2021

How verify if a attribute exists in a object?

I create a object.

SET nlsoref=##class(%SYS.NLS.Locale).%New()

So I want to verify if the attribute 'test' exists in the object, I tryied use $data

write $data(nlsoref.test)

But show this message:

<PROPERTY DOES NOT EXIST> *test,%SYS.NLS.Locale

Someone can help?

Best regards, Flávio.

0
0 664
Discussion (7)4
Log in or sign up to continue

You changed the question, my answer was for a DynamicObject/Array.
For regular classes you use:
##class(%Dictionary.CompiledProperty).%ExistsId(ClassName_"||"_PropertyName)
or just use a Try-Catch construct.

Sorry, I changed to be more clear, but don't solve my problem. This command show to me the error below:
 

<METHOD DOES NOT EXIST> *%IsDefined,%ZEN.proxyObject

Have you tried the %IsDefined method?

set a={}

if a.%IsDefined("test") write "defined"

set a.tset=1

if a.%IsDefined("test") write "defined"

I think Ben meant:

set a.test = 1

I'd typically use:
$$$comMemberDefined("%SYS.NLS.Locale",$$$cCLASSproperty,"test")

By the looks of it, the original question applied $DATA to an element of the %DynamicAbstractObject classes.  The $DATA and $GET functions can only be applied to Multidimensional variables.  All ObjectScript local and global variables are Multidimensional.  By default a property variable of a class object is not multidimensional unless the property default is over ridden with the [ Multidimensional ] attribute.  The %DynamicAbstractObject classes provide no way of supporting elements which are Multidimensional.  If dynobj is an object reference to a %DynamicObject then dynobj.%Get(key) is defined for all possible string values of 'key'.  If dynobj.%GetTypeOf(key) returns "unassigned", or "null", then dynobj.%Get(key) will return the empty string value.

Currently, $DATA(dynobj.keyname) signals <PROPERTY DOES NOT EXIST> which is what is signaled for by all classes if the Property 'keyname' does not exist.  In a future IRIS release it will report

<INVALID CLASS> *Class '%Library.DynamicObject' does not support MultiDimensional operations

The correct way to see if keyname "test" exists in a %DynamicObject is to evaluate (dynobj.%GetTypeOf("test") '= "undefined") .