Is there a proper way of checking if a variable is uninitialized or empty?
As a workaround I've been comparing with results from gen_empty_obj
to see if its empty, but I have no idea how to check if a variable was initialized.
Asked
Active
Viewed 168 times
1 Answers
1
Here are some basic checks, using try-catch statement. Made two scenarios for demo, but you could wrap those checks in a procedure to make it more readable.
* Results of 'testObject' checks
testObject_IsEmpty := true
testObject_IsInitialized := true
* Results of 'testRegion' checks
testRegion_IsEmpty := true
testRegion_IsInitialized := true
***
* Test scenario #1 :: 'testObject' and 'testRegion' are not initialized for some reason.
*
***
* Added this if-statement to make it possible to compile the code.
* The 'testObject' and 'testRegion' will be visible but not initialized
if (false)
gen_empty_obj (testObject)
gen_empty_region (testRegion)
endif
* Try-catch will throw an exception when the variables are not initialized.
* otherwise, the try-statement will run correctly and assign if the variables are empty.
try
count_obj (testObject, count)
testObject_IsEmpty := count <= 0
area_center (testRegion, area, _, _)
testRegion_IsEmpty := area <= 0
catch (Exception)
testObject_IsInitialized := false
testRegion_IsInitialized := false
endtry
stop()
***
* Test scenario #2 :: 'testObject' and 'testRegion' are initialized, but they are empty.
*
***
* Initialize 'testObject' and 'testRegion', but they are still empty.
gen_empty_obj (testObject)
gen_empty_region (testRegion)
* Try-catch will throw an exception when the variables are not initialized.
* otherwise, the try-statement will run correctly and assign if the variables are empty.
try
count_obj (testObject, count)
testObject_IsEmpty := count <= 0
area_center (testRegion, area, _, _)
testRegion_IsEmpty := area <= 0
catch (Exception)
testObject_IsInitialized := false
testRegion_IsInitialized := false
endtry

Kroepniek
- 301
- 1
- 7