Adding objects back into a collection, after removing them.
Posted on 2003-03-10
This is the bare bones of what the code looks like (the number 100 is another variable, but was changed to a constant for simplicity's sake).
For intLoopIndex = 2 to 100
Load boxes(intLoopIndex) 'Load a new box into the control array.
Set boxes(intLoopIndex) = Hexy.Controls.Add("vb.picturebox", "Hex" & intLoopIndex) 'Set the box to a new control
boxes(1) was created at design time, and is not removed until the program unloads. As you can see, as boxes are added to the array, each one is named "Hex2", "Hex3", "Hex4", and so on.
Later, when I need to start over, I use something along the lines of:
for index = 2 to 100
And, this is all fine, until I try to recreate new boxes to replace the old ones (a new array of 2 to 100). However, the compiler complains that the name "Hex2" already exists. It sounds like the boxes are being removed, but their names are still loaded in memory, or something along those lines.
How do I correct this?