2

When declaring a variable to blank (before a loop for example), it is sometimes done as "" or Empty. Also, when checking the value, it is sometimes used with "(Not IsEmpty(variable))" and "variable <> Empty". Is it better to use one vs another and can it cause any issues using it the wrong way?

Ex1:

  • fileNameDate = Empty
  • fileNameDate = ""

Ex2:

  • If (Not IsEmpty(fileNameDate)) Then
  • If fileNameDate <> Empty Then
  • If fileNameDate <> "" Then

Thanks!

------------Update-------------

Note that my question is not asking for the difference between Null, Empty, and Nothing. I'm simply concentrating on the "Empty" part and whether it's the same things as writing "". For the most part, I have received similar results when interchanging the two, but I dont know if it's just the examples I used. For example, the following confused me some.

My code:

 Dim x, y, z

'Option1 (Do not set x to anything)

'Option2
'x = Empty

'Option3
'x = ""

If x = "" Then
'Action1
End if

If x = Empty Then
'Action2
End if

If IsEmpty(x) Then
'Action3
End if
  • If I go with Option1 and just not define "x", all three actions will occur.
  • If I go with Option2 and set x = Empty, all three actions will also occur
  • But if I go with Option3 and set x = "", only Action1 and Action2 occurs.

Why?

Yevgen
  • 767
  • 2
  • 9
  • 22
  • Possible duplicate of [String is not null, empty, or empty string](http://stackoverflow.com/questions/26107006/string-is-not-null-empty-or-empty-string) – user692942 Nov 15 '16 at 01:17
  • It's also a question that will raise a lot of opinion based answers which makes it off-topic. Please read [ask] before posting. – user692942 Nov 15 '16 at 01:19
  • 1
    Personally I find using a simple length test like `Len(fileNameDate & "") > 0` works wonders and avoids having to check for blank, null etc. – user692942 Nov 15 '16 at 01:21
  • @Lankymart Thanks. This is a nifty suggestion that I will save. – Yevgen Nov 15 '16 at 06:40

1 Answers1

5

Some considerations:

fileNameDate = Empty  ' The same as just declaring Dim fileNameDate
IsEmpty(fileNameDate) ' = True

Is not the same as:

fileNameDate = ""
IsEmpty(fileNameDate) ' = False

I think the function IsEmpty()is misnamed, because it checks if the variable has been initialized, not if it's actually empty.

karliwson
  • 3,365
  • 1
  • 24
  • 46
  • It actually doesn't matter what type it is as VBScript is typeless, everything is a `Variant` which means checking if a date, a integer, a long and a string all work the same way. I could write `Len(someVariable & "") > 0` and it would still work for any of those. In effect your explicitly casting it to string so in the case of dates and numeric you would need to cast them back. – user692942 Nov 15 '16 at 01:26
  • karliwson, I thought your last line answered my question. However, I tested by setting the variable to Empty and then checking with IsEmpty() and still received True (see update in description). Shouldn't it be False since I did initialize my variable with "Empty" or setting to "Empty" just means to uninitialize (clear any value in there and reset)? – Yevgen Nov 15 '16 at 05:47
  • @Yevgen, By setting the variable to `Empty` you are not initializing it, you are doing the opposite: de-initializing. It may be confusing at first, but think you're telling VBScript that the variable is empty (not initialized), so, naturally it will say it's empty when you ask. – karliwson Nov 15 '16 at 05:49
  • Okay, I think that makes sense. Then checking with "x = Empty" seems to be a more universal check option since it will return true if the variable 1) Has not been initialized 2) If it was set to 0 length (i.e "") 3) If variable was set to "Empty". This answers my questions of what I should use. Thanks @karliwson ! – Yevgen Nov 15 '16 at 05:59
  • 1
    Except for case 2. If the string is set to `""` it will return False, because it has been set. If you don't need to know specifically if the variable was set or if it's just blank (eg: `x = ""`, you can use @Lankymart's suggestion: `Len(x & "") > 0` – karliwson Nov 15 '16 at 06:13