>
>Hmm, good question. I'm not sure that fundamentally there is any difference in your two snippets. Since it's possible that the code in the loop could change either this.TreeView1.Nodes.Count or nCount, then the question really boils down to whether the value is re-evaluated for each loop, or if it's fixed at the start of the first loop. I don't know the answer to that off the top of my head.
>>
>>It's probably infinitesimally quicker to use nCount instead of this.TreeView1.Nodes.Count, assuming there are enough loops to offset the overhead of declaring and initializing the extra variable. There could be a large speed gain if TreeView1 is an ActiveX; my understanding is that COM calls like that are expensive and that your second snippet is best practice in these cases.
>>
>>UPDATE: Docs for the C# FOR command:
>>
>>"The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false."
>>
>>Seems to me, this means the logical expression is evaluated before each loop, not just once.
>>
>
>It is not COM but a standard .NET 2.0 TreeView. As I suppose it is evaluated every time it should be faster on the second example (variable).
>Just wanted to confirm.
Actually, this doesn't answer the question of whether the value really is evaluated each time. You could test fairly easily with something like
int nCount = 5;
for (int i = 0; i < nCount & i < 10; i++)
{
nCount++;
}
< do something that shows the value of nCount >
Regards. Al
"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov
Neither a despot, nor a doormat, be
Every app wants to be a database app when it grows up