Some time ago when I was still learning programming, I learned that the lines of code execute in the order in which they appear unless otherwise branched by a conditional statement or something similar to that.
I have actually seen cases where statements do not execute in the order in with they appear. I know this is possible when using events, but I was not using them.
There is a function that I have used, it basically does three things:
1) Update a temporary table
2) Loads a report (crystal report) that uses the temporary table
3) Clear all records
So the function looks like this;
Private Function ShowSummary()
UpdateTable
ShowReport
ClearTable
End Function
The problem is that the when the report appears, it’s empty! If I stop the program just before the report is shown I find that the table has the updated records.
So then I paused the program just before the report is shown and then continued . It worked – the report showed all the records in the database. So somehow, the ShowReport statement executed before the UpdateTable statement was completed. So to make a permanent solution I inserted delaying statements between the two statements and function looked similar to the one below.
Private Function ShowSummary()
UpdateTable
For j = 1 to 1000000 ‘delaying loop
Next j
ShowReport
ClearTable
End Function
I’m aware that the updating statement takes a long time because it has to do some data access, but why does the next line of code execute before it has finished?
For every programmer that had a similar problem I prescribed this method. It worked fine. Now I feel ashamed. WhenI should be striving for faster algorithms as a programmer, I’m actually delaying them. This is clearly bad – there must be another solution.
What really causes thi