07-18-2023 08:17 PM
Hello,
Not for the first time, I'm finding IsNull not behaving as it should. The following code produces two unexpected results. Firstly, the variable contents are ignored in the second Logfilewrite call and, secondly, IsNull returns false when the variable contains NoValue, but returns true when passed NoValue directly. This is contrary to what I've been told on this forum in the past.
Dim var
Logfilewrite var
Logfilewrite "1: " & var
Logfilewrite IsNull(var)
Logfilewrite IsNull(NoValue)
NOVALUE
1:
FALSE
TRUE
Can someone tell me what the correct way is to determine whether a variable has been filled or remains unassigned please. I have a lot of code that use IsNull and I'm now finding it doesn't work as expected.
Regards,
Simon.
Solved! Go to Solution.
07-19-2023 01:25 AM
Hello Simon,
in your example you are using an unassigned variable. A variable that you have not yet used in the vbscript is empty. It is not null. You can query this empty state with IsEmpty:
LogfileWrite IsEmpty(var)
In your second statement, VBS tries to turn the unassigned variable into a string. Since it has no content, it becomes an empty string. As a developer you can be glad here that it doesn't come to an error, so you don't have to handle this case :-).
NoValue is a reserved variable containing NaN (Not a Number). Thus, the query IsNull(NoValue) must of course return True.
Regards,
AnJalpaka
07-19-2023 02:26 AM
Thanks AnJalpaka,
I'm afraid I don't agree with your suggestion that I should be glad I don't have to deal with this case. I would be much, much more glad if things were consistent. You don't mention line 1, which does not seem correct to me if it returns NoValue when the variable is empty.
If line 1 of the code tells me that var has NoValue, then I'm obviously going to expect line 3 to return True.
So, to get a correct understanding of the state of things, I have to correctly select the way I diagnose the code or I could easily be misled. There's too much luck in there for my liking.
Regards.
07-19-2023 03:10 AM
Hi Simon,
In VBS an unused variable is empty. This is shown by the statement with IsEmpty. If an uninitialized variable is checked with the VBS function IsNull, False is returned because it is not null but empty.
On the first line, an empty variable is passed directly to the DIAdem function LogfileWrite without using a VBS function (IsEmpty, IsNull or an implicit conversion on "& var"). DIAdem checks for the content and outputs NOVALUE in case of an empty variable, because from the point of view of DIAdem (not VBScript) this variable has no value.
In the case of the second logfilewrite, an automatic conversion by VBScript takes place, which passes the converted text to the DIAdem function LogfileWrite which is an empty string after "var".
07-19-2023 03:40 AM
If you always initialize your variables, it is sufficient to work with IsNull. Otherwise IsEmpty must also be used. Once a variable is initialized, IsNull will be enough to make sure if the variable is null or novalue.
When using objects, it is necessary to work with the is operator and the comparison on Nothing:
set var = Nothing
LogfileWrite IsNull(var)
LogfileWrite var is Nothing
IsNull returns FALSE and is Nothing returns TRUE.
07-19-2023 04:28 AM
Hi AnJalpaka,
Thanks for the information. Understanding the behaviour of line 1 and 2 seem to me to require insider knowledge. For basic level coders like me, who are also heavily reliant on the content of DIAdem's help files, there's little chance of working this out. For this reason, I still consider this behaviour an inconsistency.
I am also somewhat reluctant to consider this issue resolved! However, since you have kindly provided an answer I will accept your responses as the answer to my query.
Regards.