Ben Poole

“It is a very sad thing that nowadays there is so little useless information.”

Tip #9: empty variants

Add a comment

Introduction

This is a quick and easy tip for a very annoying issue in R5 Lotusscript. I often find myself doing things that involves the use of variants (usually with Prompts, Dialogboxes and PickLists). Being anal a conscientious developer, I do stuff like this:

Dim theBloodyPrompt As Variant

Yes, it's always good practice to declare everything, but this innocuous looking baby can really cause problems. In order to fully explain what I'm getting at, let me expand upon this sample code:

Example prompt

Dim workspace As New NotesUIWorkspace
Dim theBloodyPrompt As Variant

theBloodyPrompt = workspace.Prompt(PROMPT_OKCANCELLISTMULT, db.Title, K_PROMPT, "", mySpecialList)

You did error trap didn't you?

Like all good coders, we should trap the prompt in case the user hits "Cancel", or doesn't actually select anything before hitting "OK." If we don't subsquent code that deals with the theBloodyPrompt variable will fail, giving out helpful messages like Type mismatch. So, the helpful developer does something like this (bearing in mind that this kind of prompt returns an array):

If theBloodyPrompt = ""...
OR
If IsEmpty(theBloodyPrompt)...
OR
If IsArray(theBloodyPrompt)...
OR
If IsNull(theBloodyPrompt)...

None of these will work. Look in the debugger "Variables" pane when the prompt is failing, and every time you will see that theBloodyPrompt is empty. No value assigned whatsoever. Designer Help isn't very helpful: The return value is a variant containing a string array if you press OK; and Empty if you press Cancel.. Yeah, fine, but where does that leave me?!? I tried testing for empty!

The solution

Ah, but in true Lotus Notes fashion, there's a wee secret to making this work properly, and it all lies in the declaration. Remember our declaration of theBloodyPrompt above? Replace it with this:

Dim theBloodyPrompt

A subtle, but important, difference. That'll sort it. Now, in testing this variable, you can do this safe in the knowledge it'll work this time:

If Isempty(theBloodyPrompt) Then...

So simple! The secret is not to assign a data type. Which makes sense. Not. [smiley anger]

   Tip #9: empty variants (Ben Poole 29 Jan 2003)
   RE: Tip #9: empty variants (wasabi 30 Jan 2005)
   RE: Tip #9: empty variants (Eric Rehnke 12 Jul 2005)
   RE: Tip #9: empty variants (Eric Rehnke 13 Jul 2005)

Add a comment