The problem I have is related to the debugging phase. You get a bug, you yell "WTF !" you look in your code: nothing. You check the logic of your reasoning, the way data is handled: nothing. Then you decide to put a break point somewhere and run your program. Execution stops, you take a peek at the data, you see what the problem. you go back the code, you change a line or two and TADAM. Magic, problem solved ! But I did that, and nothing at all. Heres a sample of the broken code ( I thought I had it saved somewhere but it seems that part of code was deleted, here is something that should look like it. )
Sub something()
For Each c as heroTile in Me.Controls
Me.controls.remove(c)
Next ' Breakpoint right at this line.
msgbox("Test Test Test")
End Sub
I inserted a break point at the Next keyword. And the msgbox method is for testing purposes, you will see why in a moment. After execution, the loops go through the controls and removes them. ( This will actually return an error, but this is just a sample to illustrate my problem ) When he hits the next, after every iterations are finished or while he is doing it. Execution stops. You heard me right, it just stops !! It will not budge, I can't make the execution go forward because he will NOT go to the next line. The compiler just waits there and looks stupid. I repeat, msgbox is NOT shown because execution will NOT go through that line. Which admittedly is weird. It might be something I'm missing or knowledge I don't have, but without a couple of hours of research I just can't see how I will solve this. Meanwhile, I thought of something for you guys:
For Next "Remove control" error.
Lets say I have this piece of code in a procedure:
Your guess is a good as mine, is scrolls through every control inside the form, and removes every pictureboxes. One by one, pictureboxes will be removed. From the first to the last. Unfortunately, you will find yourself with this result:
Can you spot why ? I'll tell you why. Because the Remove() function works in a special way. I made up a little .png to explain how it works. Image looks like crap but at least I can explain what it all means.
First lets say I have 10 picture boxes numbered 0 to 9. I want them all dead. I invoke the Remove() function inside a loop and I'm done. Actually, its otherwise. First, during the very first iteration of the loop ( lets say a For Each ) he will "plug" himself on the first picturebox he sees. He will then delete the control. BUT he will take every other control in the collection and move them one place up the collection. So element 1 will be element 0, element 2 will be element 1 etc. But a For Each works in ONE way and its numerically from the first control to the last. After deleting what was contained in the first address, he will move to the second EVEN IF THE CONTENTS OF THE FIRST ADDRESS WAS FILLED UP. At the second pass, he will remove the second element ( which is originally element #3, the second one is replacing the #1). This will create the blanks you can see in my output. Each one he removes, the next one is skipped. The problem is easily resolved by using a Do Until or another mechanism to check if there is a pictureBox remaining and delete it until there is none on the form.
I'm hoping this piece will help some people when they see the same problem. This information was found using MSDN, never forget to hit the f1 key when you need help !


No comments:
Post a Comment