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?

4
  • Possible duplicate of String is not null, empty, or empty string Commented Nov 15, 2016 at 1:17
  • It's also a question that will raise a lot of opinion based answers which makes it off-topic. Please read How to Ask before posting. Commented Nov 15, 2016 at 1: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. Commented Nov 15, 2016 at 1:21
  • @Lankymart Thanks. This is a nifty suggestion that I will save. Commented Nov 15, 2016 at 6:40

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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, 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.
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 !
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.