I'm accessing a layer from the layers collection by name:
Set layerObj = layersObj( "layerNameStr")
This crashes when layerNameStr item does not exist
I seen the for loop through each item to check if it exists but that seems inefficient
There's gotta be a better ways
-catch the error?
-layer.exist function?
There are several places where assigning an object (i.e. layer, shape, etc.) can return 'nothing' or 'null'. Good programming will wrap these in some form of error catching. Example in .Net is try...catch..end try or in VBA 'on error goto something'. Remember when you build these mini-handlers to point your code back to the main handler when completed.
al
on error goto layerErrHandler
set layerObj = layersObj("layerNameStr")
layerErrHandler:
if layerObj is Nothing then
' go do something because you couldn't find it
end if
on error goto mainErrHandler
....
al
Thanks, took your advice and error caught. Would appreciate help on the handling part though:
How do I Negate the "Is" operator? In other words, what's the VBA equivalent for != in C++?
I have a getLayer function that returns nothing if the layer does not exist.
-Set layerObj = getLayer(layersObj, strSpecialty)
Then checking if nothing is returned
-If Nothing Is layerObj Then
which works fine
What if I want to negate it? I tried
-If Nothing <> layerObj and
-If Nothing Is Not layerObj
but get compilation errors of type mismatch.
What I got works, just looking for logical understanding.
you might try
if Not (layerObj Is Nothing) then
end if
if layerObj is Nothing then
' did not find the layer
else
' found the layer
end if
this is of interest to know
http://www.tek-tips.com/faqs.cfm?fid=3710
al
Thanks!
I use a lot of "loop-scanners" in my code, because reading from Visio is quite fast. I don't like using Try-Catch for things like layers or shapes, since they can execute quite slowly if the error condition pops up.
I think it depends on if your code is expecting something or not. If you are trying to get a layer, and you expect it to be there 99.5% of the time, then a try-catch is in order.
If you are going to prompt the user to create a layer if it's not there, and it might very likely not exist, you should probably scan to see if it exists. Even 50 layers will be read very quickly (especially in relation to any user interaction that might follow) so it won't hurt to loop through them.
Cool, thanks for the tips. Had no idea, looping would be faster but I did notice that error handling does take time.
Browser ID: smf
(is_webkit)
Templates: 1:
Printpage (default).
Sub templates: 4:
init,
print_above,
main,
print_below.
Language files: 1:
index+Modifications.english (default).
Style sheets: 0:
.
Hooks called: 59 (
showintegrate_autoload, cache_get_data, integrate_pre_load, integrate_load_session, integrate_verify_user, cache_get_data, integrate_user_info, integrate_load_board, cache_get_data, integrate_board_info, cache_get_data, integrate_allowed_to_general, integrate_pre_load_theme, cache_get_data, integrate_allowed_to_general, integrate_simple_actions, integrate_allowed_to_general, integrate_load_theme, integrate_pre_log_stats, cache_get_data, integrate_actions, integrate_pre_parsebbc, integrate_bbc_codes, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_pre_parsebbc, integrate_bbc_print, integrate_post_parsebbc, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general, integrate_menu_buttons, integrate_current_action, integrate_theme_context, integrate_allowed_to_general, integrate_allowed_to_general, integrate_allowed_to_general)
Files included: 25 - 925KB. (
show/home/iw0lkfe3x6cq/public_html/vgforum/index.php, /home/iw0lkfe3x6cq/public_html/vgforum/Settings.php, /home/iw0lkfe3x6cq/public_html/vgforum/cache/db_last_error.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/QueryString.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Subs.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Subs-Auth.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Errors.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Load.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Security.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Subs-Compat.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Subs-Db-mysql.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Cache/CacheApi.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Cache/CacheApiInterface.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Cache/APIs/FileBased.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Subs-Charset.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Unicode/Metadata.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Unicode/QuickCheck.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Session.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Logging.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Class-BrowserDetect.php, (Current Theme)/languages/index.english.php, (Current Theme)/languages/Modifications.english.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Printpage.php, (Current Theme)/Printpage.template.php, /home/iw0lkfe3x6cq/public_html/vgforum/Sources/Unicode/CaseUpper.php)
Memory used: 765KB.
Tokens:
post-login.
Cache hits: 7: 0.00131s for 22,301 bytes (
showget modSettings: 0.00045s - 19982 bytes, get known_languages: 0.00034s - 1277 bytes, get board_parents-0: 0.00014s - 2 bytes, get permissions:-1: 0.00010s - 50 bytes, get theme_settings-1: 0.00014s - 980 bytes, get log_online-update: 0.00010s - 10 bytes, get menu_buttons--1-english: 0.00004s - 0 bytes)
Cache misses: 1: (
showget menu_buttons--1-english)
Queries used: 9.
[Show Queries]