0

What are the differences between

Dim Str As String = ""

And

Dim Str As String = vbNullString

When should I prefer one instead of the other?

What I mean is:
if I want to clear a string (so to check if it got a value after) what choice is better?

genespos
  • 3,211
  • 6
  • 38
  • 70
  • 2
    The former is an empty string, the latter is `Nothing`. Try to call `"".Length`, you'll get 0 as expected. Try to call `vbNullString.Length`, you'll get a `NullReferenceException`. Don't use old VBA/VB6 methods or constants but .NET types, methods and operators, so `Nothing` in this case. – Tim Schmelter May 24 '16 at 13:52
  • 2
    vbNullString is vb6 legacy, the interop scenarios where it mattered are no longer supported in vb.net. So just avoid it and use Nothing instead. Or don't initialize the variable, same thing. These statements are not the same, a reference to an empty string is not the same as one that is Nothing. Which one is correct or preferable is impossible to say, it entirely depends on how you use the variable in the rest of your code. – Hans Passant May 24 '16 at 13:52

0 Answers0