var var1 (obj->getProperty(Identifier(“George”)));
String nam = var1.getProperty(“Name”, String(“Not Set”));
String bd = var1.getProperty(“Birth_Day”, String(“Not Set”));
It seems that the var1 object points to the second node of obj object.
The “nam” variable contains “John” while it should contain “George”.
Probably, I misunderstood something but I can’t figure out what’s that;
I assume the capital Obj1 is a copy mistake meaning obj1, otherwise I must ask, what Obj1 is and where it is declared…
The DynamicObject is a ReferenceCountedObject, so I think after assigning obj.setProperty("George",obj1); the obj1 still references the property “George”. So the obj1->clear() gets rid of the Name => George etc. properties and adds new properties “Name” => “John”.
obj1 inherits a ReferenceCountedObject. A ScopedPointer to that would probably try to delete it. I think that will cause problems.
I usually populate DynamicObjects like this, but I am interested in more elegant versions:
DynamicObject obj;
obj.setProperty ("George", new DynamicObject());
if (DynamicObject* george = obj.getProperty("George").getDynamicObject()) {
george->setProperty("Name","George");
george->setProperty("Birth_Day","17-05-1900");
}
obj.setProperty ("John", new DynamicObject());
if (DynamicObject* john = obj.getProperty("John").getDynamicObject()) {
john->setProperty("Name","John");
john->setProperty("Birth_Day","10-03-1983");
}
//.......
var var1 (obj.getProperty(Identifier("George")));
String nam = var1.getProperty("Name", String("Not Set"));
String bd = var1.getProperty("Birth_Day", String("Not Set"));
And on a personal note, I made it a habit never to copy a string literal, because it is hard to find errors. You can let the compiler find them by creating a static Identifier, I usually define inside the class:
class MyClass {
public:
static Identifier paramName;
// ...
};
// and on top of your cpp
Identifier MyClass::paramName ("Name");
// so you can use it
george->setProperty(paramName,"George");