Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
Using the .NET SDK 5.8.0.
I'm seeing a problem where if you remove a property from a VirtualThing's PropertyCollection then try to add the same property name back later it will throw an exception "an item with the same key has already been added".
This is very simply to replicate:
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
getProperties().Remove("test");
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
The exception will be thrown on the second call to defineProperty. Apparently the call to .Remove is not fully removing the property somehow behind the scenes. But if you examine the collection it definitely has removed it. So maybe the property name is being stored somewhere else as well?
Am I doing something incorrectly with the removal of the property? There is no other way to do it that I can see.
Solved! Go to Solution.
Hi @CSharpie.
I checked on this and found that defineProperty() creates both a property and a property definition. It appears you are removing the property, but not the definition. Try this:
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
getProperties().Remove("test");
getThingShape().getPropertyDefinitions().Remove("test");
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
Regards.
--Sharon
Hi @CSharpie.
Please explain the use case here. We're not understanding why you would create the property, immediately remove it, and then recreate it. Are you seeing any errors? Is your intent to remove the property or the value of the property?
This article references entities but the content may be helpful in your case as well.
Regards.
--Sharon
The intent is to remove the property completely. Keep in mind my example is only to show how to produce the issue. Of course no one is going to add, remove, and add all at the same time.
I realize the use case is a low probability one but the API does not seem to be working as it should. If the property has been removed there is no reason adding it back later should throw an exception.
Also, if the SDK is leaving remnants of a removed property lying around internally who knows what other issues this may create after the application has been running for a long time... memory leaks, whatever.
Hi @CSharpie.
I checked on this and found that defineProperty() creates both a property and a property definition. It appears you are removing the property, but not the definition. Try this:
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
getProperties().Remove("test");
getThingShape().getPropertyDefinitions().Remove("test");
defineProperty("test", "", BaseTypes.STRING, new AspectCollection());
Regards.
--Sharon
Hi @CSharpie.
If the previous response provided the explanation you needed, please mark it as the Accepted Solution for the benefit of others on the community.
Regards.
--Sharon
I tried this and it does solve our problem. Thanks much!