Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
BUG: With Collections or FOR EACH...
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00841263
Message ID:
00841269
Views:
21
>...or something. I am using VFP8 SP1. When items in a Collections object are holding an object reference to forms, an object variable in a FOR EACH loop will change to logical when the loop is exited. Here is the repro code:
>PRIVATE goApp
>LOCAL loForm1, loForm2, loRef
>goApp = CREATEOBJECT('AppManager')
>loForm1 = CREATEOBJECT("_Form")
>loForm1.Name = "MyForm1"
>loForm2 = CREATEOBJECT("_Form")
>loForm2.Name = "MyForm2"
>loRef = goApp.GetFormObjectRef(loForm2.Name)
>RETURN
>
>DEFINE CLASS _Form AS Form
>    PROCEDURE Init
>        goApp.Add2FormList(THIS)
>    ENDPROC
>ENDDEFINE
>DEFINE CLASS AppManager AS CUSTOM
>
>    ADD OBJECT FormList AS Collection
>
>    PROCEDURE Add2FormList
>        LPARAMETERS toForm
>        IF TYPE('toForm') <> 'O'
>            RETURN .f.
>        ENDIF
>        IF UPPER(toForm.BaseClass) <> [FORM]
>            RETURN .f.
>        ENDIF
>        LOCAL lcHWND
>        lcHWND = TRANSFORM(toForm.HWND)
>        THIS.FormList.Add(toForm, TRANSFORM(toForm.HWND))
>    ENDPROC
>    PROCEDURE GetFormObjectRef
>        LPARAMETERS tcName
>        tcName = ALLTRIM(UPPER(tcName))
>        LOCAL llFound, loForm
>        FOR EACH loForm IN THIS.FormList
>            IF UPPER(loForm.NAME) == tcName AND UPPER(loForm.BASECLASS) = 'FORM'
>                llFound = .T.
>                *!* BAM! As soon as the next line executes,
>                *!* loForm is no longer an object!
>                EXIT
>            ENDIF
>        ENDFOR
>        IF llFound AND VARTYPE(loForm) <> "O"
>            MESSAGEBOX("loForm is no longer an object!",16,"ERROR!")
>            loForm = .NULL.
>        ENDIF
>        RETURN loForm
>    ENDPROC
>ENDDEFINE
Mark,

All I've got is a huge shot in the dark based upon the way computers used to handle things back in the "dark ages".

Back then it was bad form to exit out of the middle of an iteration structures because it left junk on the run-time stack. These days it's bad form for design reason, but in the case of a FOR EACH structure there's not much choice in a situation like your's.

What I think that VFP may be doing is some internal housekeeping in order to prevent memory problems. IOW, automatically setting loForm to null or something when the loop exists. With that in mind, I'd make the following suggestion for you to try.

Instead of returning the iteration control variable (loForm), when the Forms Manager finds the form it's looking for, assign it to another variable before exiting the structure and return that.

Please let me know how this comes out.
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform