Anonymous Methods and Captures in VCL
Found an interesting debugging effect of captures today.
Picture a frame with a list and a MyValueEdit (TAdvSpinEdit to be exact). So, I’d made a bad decision to check the MyValueEdit.FloatValue in an anon method which was looped for selected elements in the list.
Firstly, the value was wrong. Even though I believed I had edited the value before the loop – the captured MyValueEdit retained the original value.
Secondly, trying to inspect MyValueEdit inside the anon method, informed me rather sternly that “MyValueEdit” is unknown.
Note to self: When in an anonymous method, minimize references to local variables outside the method. Needless to say, I am also looking at doing a proper enumerator instead of a loop that takes an anon method.