Iterating through all properties of an object

Primary tabs

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:
  • + 4
  • 1
  • 1,627
  • 1
  • 5

Answers

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

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

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).

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)