Reputation: 876
I'm creating a small piece of software that lets you add controls in runtime.
What I've done is creating a derived class from the controls, and those include a static integer counter which is increased every time an instance of that class is constructed. This lets me create a naming convention so the names never repeat itself. For example I say
this.Name = "Label"+counter;
The problem comes when I want to store this in a sort of custom serialization. Lets say that I create 3 labels: Label0, Label1, Label2 and then, I delete the Label1. Which would lead in having Label0 and Label2.
If I serialize the current state, and then I load that data to rebuild the state, the creation of labels would lead to have the counter as the value of 1, being the name of the last label "Label2". This implies that if now I want to create another label, would be "Label(counter+1)", that is, "Label2", resulting in a duplicate.
My question is, which is the best way to create a counter that results in a naming convention as simple as this and skips duplicated names?
Upvotes: 1
Views: 223
Reputation: 63065
Serialize counter with other controls, then you can load exiting controls and also create new controls by using counter value.
Upvotes: 1