PreviousNext…

Tip #9: empty variants

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. >:-|

Comments

  1. Just thought ill drop a quick comment on this , since I use variants for everything(well , almost)

    I usually just do a check :
    if not datatype(theBloodyPrompt) = 0 then do the rest of the stuffwasabi#
  2. First of all, thanks Ben for coming up with a solution to this VARIANT hell. Lotus ought to fix this, but, having been fighting with quirks such as this for almost 10 years, I won't hold my breath. The only problem I had with your approach is that it doesn't work when trying to declare private vars inside an object. You have to publicly declare the variable which means that a variable that should be private is exposed to the world.

    However, wasabi's solution works great and doesn't break the object model architecture. Who would've guessed?? And, more importantly, why the hell hasn't Lotus at least DOCUMENTED this quirk????

    But, anyway, problem solved thanks to you and Wasabi!!!

    Thanks….. some sanity is restored …. Eric Rehnke#
  3. It seemed that the solution from wasabi was working fine but the next day it decided not to work. Notes is really quirky and has lots of caching issues with v6.04.

    Today, i found that to detect whether a variant actually contains something, i have to test for dataType(variant) = 8712

    if that is true then my testing indicates that a variant has been populated…

    with Notes, there's ALWAYS something to "work around"Eric Rehnke#

Comments on this post are now closed.

About

I’m a software architect / developer / general IT wrangler specialising in web, mobile web and middleware using things like node.js, Java, C#, PHP, HTML5 and more.

Best described as a simpleton, but kindly. You can read more here.

";