Select Case references item in memory or reexecutes?

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
Say for example that there is a function GetSomething() which returns some Object. What happens when the following is performed?

Select Case GetSomething()
Case A
...
Case B
...
End Select

Will GetSomething() be reexecuted each time a Case is tested or will its result be stored in memory somewhere? So will it be executed once and then its result in memory be evaluated each time or will it be executed twice? *scratch* Anybody know?
 
GetSomething() is executed once because it is only called once.
 
To answer the question directly, it runs the function and takes the result and shoves it on the stack.... the value is then used for the comparison. So the answer is "once" ....

However, that's actualy considered a bad habit by a lot people. Primarily because it causes the very same question you raised. "Proper" coding would be to declare a variable to hold the results, then use THAT for the case.

-tg
 
Okay cool. But if the result of the method is ONLY used for the Select's comparison then is there a need to declare a variable to hold the results? Once the Select has completed the comparison and carried out the necessary tasks for a Case will the result of the method be removed from the stack, since it is not referenced by any variable and not in use?
 
Your use of the function as a case comparer is perfectly valid and "proper" as anything. The return value of the function is already allocated as its type when used, so why allocate another one if you don't need the value again?
 
Back
Top