Good idea to do it in the loop. Should have thought of that myself.
>I checked some of my routines and the object that was iterated during the FOR EACH..ENDFOR is usable after exiting the loop even if it was a collection such as this.controls. Don't know why it wouldn't be availble everytime.
>
>BTW,
>
>If you did it this way then the whole discussion might be moot <bg>.
>
>
>myform.method()
> LOCAL lo, lc
> lc = WPARENT( THIS.NAME )
> FOR EACH lo IN _VFP.FORMS
> IF UPPER( lo.NAME ) == UPPER( lc ) AND lo.baseclass='Form'
> IF lo.WINDOWSTATE = 1
> lo.WINDOWSTATE = 0
> lo.REFRESH()
> ENDIF
> EXIT
> ENDIF
> ENDFOR
>
>
>
>>Hi Tom,
>>I think you hit it. When working on a collection instead of an array it works different. Have to remember that.
>>In my example, I was finding a match. I would trace it and see where it hit the "exit" command. It ALMOST makes sense that an object could go out of scope when leaving a for next loop but not a variable. Almost but not quite, but I'll live with it.
>>
>>
>>>Bill,
>>>
>>>Maybe its because the top example is done on a collection instead of on the array like in your second example.
>>>
>>>I am not sure in the first example if you are actually achievieng the exit from the FOR..ENDFOR because of a match or is it just dropping through when its done with no match.
>>>
>>>If it is dropping through, then it would make sense that lo would not be an object any longer because the FOR..Endfor has terminated.
>>>
>>>We have always assigned our matching object to a memory variable or a property when exiting the loop upon a sucessful match.
>>>
>>>Don't have to do it that way, but I know it works.
>>>
>>>HTH
>>>
>>>>Ok, I can deal with that. Don't understand it, but I can deal with it.
>>>>But how about explaining these 2 examples:
>>>>
>>>>myform.method()
>>>> LOCAL lo, lc, loParent
>>>> lo = 'some text'
>>>> lc = WPARENT( THIS.NAME )
>>>> FOR EACH lo IN _VFP.FORMS
>>>> IF UPPER( lo.NAME ) == UPPER( lc )
>>>> loParent = lo
>>>> EXIT
>>>> ENDIF
>>>> ENDFOR
>>>> IF TYPE( 'loParent.NAME' ) = 'C' ;
>>>> AND loParent.WINDOWSTATE = 1
>>>> loParent.WINDOWSTATE = 0
>>>> loParent.REFRESH()
>>>> ENDIF
>>>>
>>>>Obviously VFP is working with the local variable that I set to 'some text', it was changed from character to .F.
>>>>I'm guessing that means that a for each loop confiscates a variable from the method for it's own use, but doesn't restore the value back when it's done, instead it just sets it's value to .f.
>>>>
>>>>LOCAL lc, la( 5 )
>>>>la( 1 ) = 'A'
>>>>la( 2 ) = 'B'
>>>>la( 3 ) = 'C'
>>>>la( 4 ) = 'D'
>>>>la( 5 ) = 'E'
>>>>FOR EACH lc IN la
>>>> IF lc = la( 3 )
>>>> EXIT
>>>> endif
>>>>ENDFOR
>>>>? lc
>>>>
>>>>So I guess a for each loop only confiscates variables and sets them to false if I'm cycling through objects. Cycling through characters leaves it just the way I would expect.
>>>>
>>>>Is there any logic to that? or is it just a "feature" I have to live with?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>>Your FOR EACH...ENDFOR creates and uses its own copy of the lo var, not the one from your Local declaration.
>>>>>
>>>>>You have to transfer the object reference to a an actual memory variable like you have done in your second example and that is why it works.
>>>>>
>>>>>Thats also why the lo variable continures to show a .f. in the locals window.
>>>>>
>>>>>HTH,
>>>>>
>>>>>
>>>>>>I lost something here.
>>>>>>Why is it out of scope? The locals window still shows that it exists, just the wrong type(). I assume that means that it's still in scope.
>>>>>>
>>>>>>>You have gone out of scope in the FOR...ENDFOR in the first example without making a memory variable reference to the lo object being evaluated.
>>>>>>>
>>>>>>>
>>>>>>>>I'm using VFP 7.0
>>>>>>>>
>>>>>>>>This doesn't work
>>>>>>>>
>>>>>>>>myForm.method()
>>>>>>>> LOCAL lo, lc
>>>>>>>> lc = WPARENT( THIS.NAME )
>>>>>>>> FOR EACH lo IN _VFP.FORMS
>>>>>>>> IF lo.NAME == lc
>>>>>>>> EXIT
>>>>>>>> ENDIF
>>>>>>>> ENDFOR
>>>>>>>> IF TYPE( 'lo.NAME' ) = 'C' ;
>>>>>>>> AND lo.WINDOWSTATE = 1
>>>>>>>> lo.WINDOWSTATE = 0
>>>>>>>> ENDIF
>>>>>>>>
>>>>>>>>but this does
>>>>>>>>
>>>>>>>>myForm.method()
>>>>>>>> LOCAL lo, lc, loParent
>>>>>>>> lc = WPARENT( THIS.NAME )
>>>>>>>> FOR EACH lo IN _VFP.FORMS
>>>>>>>> IF lo.NAME == lc
>>>>>>>> loParent = lo
>>>>>>>> EXIT
>>>>>>>> ENDIF
>>>>>>>> ENDFOR
>>>>>>>> IF TYPE( 'loParent.NAME' ) = 'C' ;
>>>>>>>> AND loParent.WINDOWSTATE = 1
>>>>>>>> loParent.WINDOWSTATE = 0
>>>>>>>> ENDIF
>>>>>>>>
Bill Morris