7

The code below returns TRUE when I expected it to return FALSE.

Why does it return TRUE? I Expect nothing to set the value of the string to null, not empty (According to msdn)

CodeingGround sample

Module VBModule

    Sub Main()
        dim x as String
        x = nothing
        console.writeline(x = string.Empty)
    End Sub

End Module

Nothing (Visual Basic)

Represents the default value of any data type. For reference types, the default value is the null reference.

***EDIT**** Nothing = String.Empty (Why are these equal?)

Nothing in VB.net is the default value for a type. The language spec says in section 2.4.7:

Nothing is a special literal; it does not have a type and is convertible to all types in the type system, including type parameters. When converted to a particular type, it is the equivalent of the default value of that type.

So, when you test against String.Empty, Nothing is converted to a string, which has a length 0. The Is operator should be used for testing against Nothing, and String.Empty.Equals(Nothing) will also return false.

Per then comment,

when converted to a particular type, it is the equivalent of the default value of that type.

The default value for a string is null. I dont understand why that answer was accepted.

gh9
  • 10,169
  • 10
  • 63
  • 96
  • Interesting. `String.Empty = Nothing` returns `true` whereas `String.Empty.Equals(Nothing)` or `String.Empty Is Nothing` return `false`. – Tim Schmelter Jul 22 '15 at 14:46
  • That is what I found also. – gh9 Jul 22 '15 at 14:49
  • Even `"" = Nothing` is `true` but `" " = Nothing` is `false`. – Tim Schmelter Jul 22 '15 at 14:51
  • There's a good explanation for this [here](https://stackoverflow.com/questions/2633166/nothing-string-empty-why-are-these-equal) (check the comments on accepted answer) – Saragis Jul 22 '15 at 14:56
  • It's how you are evaluating it in `x = string.empty`. The `x` is nothing which mean's it can convert to any type and do the comparison... The compiler handle's this. You can test this, make it an object and run it, it does the same thing. – Trevor Jul 22 '15 at 14:58
  • @Saragis: that's not what i call a _good explanation_ but at least it is one hidden in the comments. The question is: where is it documented? – Tim Schmelter Jul 22 '15 at 15:01
  • Also you are using `=` for the comparison that's wrong use `Is`... Change it to `Is` and see what you get :) Your not using the correct operator for comparison... – Trevor Jul 22 '15 at 15:02
  • When you use `=` it's converting that `Nothing` against `string.empty` which in turn is **true** – Trevor Jul 22 '15 at 15:03
  • @436f6465786572: that is what OP has noticed, the question is where it is documented. You're right that `Is Nothing` is the correct way, but it's not clear why `= Nothing` returns `True` since the default value for `String` is `null`. – Tim Schmelter Jul 22 '15 at 15:05
  • Here's at least [some official documentation](https://msdn.microsoft.com/en-us/library/ms233957.aspx). Doesn't say _why_ though. – James Thorpe Jul 22 '15 at 15:06
  • @TimSchmelter Well I meant good as in _interesting_. I agree, would like to see if Microsoft indicates this anywhere in the specification or something – Saragis Jul 22 '15 at 15:06
  • Also [this page](https://msdn.microsoft.com/en-us/library/0x9tb07z.aspx) (that was actually linked in the OP): _"For strings in Visual Basic, the empty string equals Nothing. Therefore, "" = Nothing is true."_ – James Thorpe Jul 22 '15 at 15:08
  • 2
    Finally i have found documentation for this. I guess this is one of the things which were done due to backwards compatibility. [`String.Equality Operator`](https://msdn.microsoft.com/en-us/library/system.string.op_equality(v=vs.110).aspx): _"The Visual Basic compiler does not resolve the equality operator as a call to the Equality method. Instead, the equality operator wraps a call to the `Operators.CompareString` method."_ Unfortunately [there](http://tinyurl.com/pylrbft) is not mentioned explicitly that `Nothing` is treated as `String.Empty`. – Tim Schmelter Jul 22 '15 at 15:16
  • 4
    [Decompiled](http://stackoverflow.com/a/12198082/284240) `Operators.CompareString` reveals the secret: `if (Left == null) return Right.Length == 0 ? 0 : -1; else if (Right == null) { return Left.Length == 0 ? 0 : 1; }...` – Tim Schmelter Jul 22 '15 at 15:22

1 Answers1

3

Difference between C# and VB.Net string comparison

The above post explains the answer clearly, credit goes to Tim Schmelter in the comment section for finding the above post

Per Tim Schmeleters comments

it is called from the vb compiler as the documentation states in String.Equality Operator

Community
  • 1
  • 1
gh9
  • 10,169
  • 10
  • 63
  • 96
  • That answer shows only a decompiled version of Operators.CompareString. It is incomplete if you dont mention that it is called from the vb compiler as the documentation states in String.Equality Operator. – Tim Schmelter Jul 23 '15 at 06:27