How to check the content of a Variant variable?

Situation:

I have a situation where I have a variable of the type Variant (called PrimaryKey). The var has 3 dimensions.

Now I need to assign the values of this variant to three fields in my table called “Primary Key 1”, “Primary Key 2” and “Primary Key 3”.

First I had this code:

“Primary Key 1” := PrimaryKey[1];
“Primary Key 2” := PrimaryKey[2];
“Primary Key 3” := PrimaryKey[3];

But if the PrimaryKey var only contains 1 key, then I got an error here.

7288.error.png

So I then created var of the Integer type called NoOfKeys. This var contains the number of how many keys/fields the PrimaryKey contains.

“Primary Key 1” := PrimaryKey[1];
IF NoOfKeys >= 2 THEN
“Primary Key 2” := PrimaryKey[2];
IF NoOfKeys >= 3 THEN
“Primary Key 3” := PrimaryKey[3];

That worked fine. Except if one of the keys is actually empty.

So what I like to know is, if there a way to check if a variant is empty. I tried with FORMAT(PrimaryKey[1]) but this will also give the same error.

Hi Erik,

As i know it, there is not such a check.
It would sometimes be handy to have something like VariantVar.HASVALUE, similar to VariantVar.ISTEXT OR VariantVar.ISBOOLEAN…

You can’t use COMPRESSARRAY either.

Maybe you can start out by assigning a dummy value to all dimensions in the array, and then check if the value is different from that dummy value.
Or if you know that it can only contain a given type of value (say it can only be assigned a text value) you could maybe use VariantVar.ISTEXT, as this one would return false if nothing has been put in the dimension at all, and true if a text value has been assigned.

have you trried myvariant.ISTEXT or . myvariant.ISCODE ?

Yes tried but not good enough!

But I found a solution:

Ok := EVALUATE(Var,String);

IF Ok THEN
IF String = ‘’ THEN [empty]

I’ve actually found a much better way to check this.

There is a function called ISCLEAR. In the documentation it says it’s for checking if a AUTOMATION variable is cleared or not. But it also works fine with VARIANT variables, at least in NAV 5.0 SP1!

Back to this one again!

Neither EVALUATE nor ISCLEAR actually worked in production, despite my initial tests showed that it worked.

Instead Rashed’s suggestion actually brought me in the right direction:

The soluton was creating a “simple” function like this:


IsVarEmpty(VAR VariantVar : Variant) : Boolean
EXIT(NOT (
VariantVar.ISCODE OR
VariantVar.ISTEXT OR
VariantVar.ISINTEGER OR
VariantVar.ISDECIMAL OR
VariantVar.ISBOOLEAN OR
VariantVar.ISOPTION OR
VariantVar.ISCHAR OR
VariantVar.ISDATE OR
VariantVar.ISTIME));