Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Testing for success of CreateObject?
Message
De
30/04/2009 14:42:18
 
 
À
30/04/2009 14:28:28
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Divers
Thread ID:
01397276
Message ID:
01397295
Vues:
83
General error handler usually shows standard error message, aborts application and records error number, program module name and program line number to database. In your particular example you could embrace CreateObject line in TRY... ENDTRY, though I don't see need in it if 'job_info' class is part of your project, i.e. it will be compiled into executable, so any typos would be found on testing stage.

>So, how would my general error handler allow me to deal with me making a typo in the class name string?
>
>How do I use gerenal error handler to know that something went wrong with the CreateObject() call and do a Return out of the method before I continue on trying to work on an object that doesn't even exist.
>
>Can you look at my posted code and show me what I would used insted of my < type('loSearch')='O' > test.
>
>
>Select Job_Info
>
> loSearch=createobject('searchcustom', 'Job_Info')
>
> if type('loSearch')='O'
>  loSearch.SearchClass1.cboFields2.value='QUOTED_BY'
>  loSearch.SearchClass1.type=2
>  loSearch.SearchClass1.init()
>  loSearch.show(1)
>  loSearch.release()
> else
>  messagebox('Error creating search object.', 0, 'Notice:')
> endif
> 
> goto bottom
> 
> thisform.recordPointerMovedParent()
>
>
>
>
>
>
>>Your program is supposed to have general error handling routine. Special cases are needed usually in case of some external factors, e.g. you try CreateObject('Word.Application') and you are unsure if MS-Word is installed on every user computer, and you want to alert user right away.
>>
>>>Is this overkill, or do most programmers actually do this with every CreateObject request? Or should I just pay attention to what I am doing and rely on my manual testing to make sure I did it correcty?
>>>
>>>
>>>
>>>>Wrap CREATEOBJECT() with TRY...ENDTRY
>>>>
>>>>>I thought it would be a good idea to test for the success of a CreateObject call, and handle the case that would occur if it does not find the class for some reason. So, my code looks like what I show below.
>>>>>
>>>>>However, the problem is that you get a run-time error if the class 'searchcustom' is not found, so my test for < type('loSearch')='O' > is never executed . So, how should I test for the (unlikely, but possible) event that the requested cannot be found?
>>>>>
>>>>>
>>>>>
>>>>>Select Job_Info
>>>>>
>>>>> loSearch=createobject('searchcustom', 'Job_Info')
>>>>>
>>>>> if type('loSearch')='O'
>>>>>  loSearch.SearchClass1.cboFields2.value='QUOTED_BY'
>>>>>  loSearch.SearchClass1.type=2
>>>>>  loSearch.SearchClass1.init()
>>>>>  loSearch.show(1)
>>>>>  loSearch.release()
>>>>> else
>>>>>  messagebox('Error creating search object.', 0, 'Notice:')
>>>>> endif
>>>>> 
>>>>> goto bottom
>>>>> 
>>>>> thisform.recordPointerMovedParent()
>>>>>
>>>>>
Edward Pikman
Independent Consultant
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform