Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
BUG: With Collections or FOR EACH...
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00841263
Message ID:
00841269
Vues:
22
>...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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform