Written by

Senior Developer Support Engineer at InterSystems
Question Peter Steiwer · Dec 12, 2016

Iterating through all properties of an object

Is there a better way to iterate through all properties of an object than the following? Perhaps without needing to open a definition of the class but directly against the object?


SAMPLES>set t=##class(HoleFoods.Transaction).%OpenId(1)                          
SAMPLES>set def=##class(%Dictionary.ClassDefinition).%OpenId("HoleFoods.Transaction")
SAMPLES>for i=1:1:def.Properties.Count() {  w def.Properties.GetAt(i).Name_":"_$property(t,def.Properties.GetAt(i).Name),!  }
Actual:0
AmountOfSale:4.95
Channel:2
Comment:
DateOfSale:62373
Discount:0
Latitude:
Longitude:
Outlet:35@HoleFoods.Outlet
Product:37@HoleFoods.Product
TargetAmount:
UnitsSold:1
ZipCode:

Comments

Ben Spead · Dec 12, 2016

For debugging or programmatic purposes?  If debugging / interactive you can just zwrite the variable to see the properties.

0
Peter Steiwer  Dec 12, 2016 to Ben Spead

For the programmatic purpose of comparing two instances of the same object (I was hoping to keep the use case separate from this post in order to get a good generic example of iterating through properties of an object - but I can go into more details if needed).

0
Eugene Karataev · Dec 13, 2016

Better way - %Library.CompiledClass instead of %Dictionary.ClassDefinition. Cache creates objects by %Library.CompiledClass which ar image of %Dictionary.ClassDefinition on compilation moment.

0
Stephen Canzano · Dec 13, 2016

A couple ideas

If it's for debugging I use $System .OBJ.Dump(oRef)

If it's in application code I ran across something that looked like

While prop '= "" {
//  Use global lookup instead of %Dictionary.Class query so users don't need privileges on that table
Set tPropClass = $$$comMemberKeyGet(sourceClass,$$$cCLASSproperty,prop,$$$cPROPtype)
 

0