23

I ran across this and was wondering if someone could explain why this works in VB.NET when I would expect it should fail, just like it does in C#

//The C# Version

struct Person {
    public string name;
}
...
Person someone = null; //Nope! Can't do that!!
Person? someoneElse = null; //No problem, just like expected

But then in VB.NET...

Structure Person
    Public name As String
End Structure
...
Dim someone As Person = Nothing 'Wha? this is okay?

Is Nothing not the same as null (Nothing != null - LOL?), or is this just different ways of handling the same situation between the two languages?

Why or what is handled differently between the two that makes this okay in one, but not the other?

[Update]

Given some of the comments, I messed with this a bit more... it seems as if you actually have to use Nullable if you want to allow something to be null in VB.NET... so for example...

'This is false - It is still a person'
Dim someone As Person = Nothing
Dim isSomeoneNull As Boolean = someone.Equals(Nothing) 'false'

'This is true - the result is actually nullable now'
Dim someoneElse As Nullable(Of Person) = Nothing
Dim isSomeoneElseNull As Boolean = someoneElse.Equals(Nothing) 'true'

Too weird...

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
hugoware
  • 35,731
  • 24
  • 60
  • 70
  • I just stumbled across while porting some VB.NET to C# - makes me hate VB.NET that much more ;). Thank you for digging into this, and thanks to those that answered. – Charles Dec 06 '10 at 21:38
  • See also http://stackoverflow.com/questions/5869661/why-can-i-not-check-if-datetime-is-nothing – jeroenh Apr 25 '14 at 06:51
  • @Charles, should I hate C# for being different from VB? Some people solve problems. Some blame languages for being different. – Dima Aug 27 '16 at 12:07

5 Answers5

30

If I remember correctly, 'Nothing' in VB means "the default value". For a value type, that's the default value, for a reference type, that would be null. Thus, assigning nothing to a struct, is no problem at all.

BFree
  • 102,548
  • 21
  • 159
  • 201
12

Nothing is roughly equivalent to default(T) for the relevant type. (Just checked, and this is true for strings as well - i.e. Nothing is a null reference in the context of strings.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Yes, but last I checked, the empty string is also Nothing. Either `IsNothing("")` or `"" Is Nothing` returns `True`. Maybe it's both. If it's not both, then the discrepancy is terrible. –  Apr 20 '11 at 16:55
1

I tried to search for it on MSDN but couldn't find anything relevant on the VB side. When searching for "struct" on C#, it clearly returns that it's a Value Type and can't be assigned null since... it's a value.

However, when looking on VB.NET keyword "Structure" it doesn't say "Value Type". Instead it says

The Structure statement defines a composite value type that you can customize.

So... object?

That would be my guess. I would like to have references to this behavior but couldn't find any.

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
  • No, object would be `Class` , which is null as default. Structure is a complex value type that can be built up of several values, and even methods. The practical difference to `Class` is that `Structure` is initialized with an actual instance. Initializing it with `Nothing` just specifies that it should be set to its default value, which is NOT null (or as we usually say in VB: Nothing) – awe Dec 11 '09 at 07:15
0

Also, structs are value types (much like int, char, etc.) and thus are non-nullable.

Dana
  • 32,083
  • 17
  • 62
  • 73
  • Well using Nullable or ? (C#), you can make a struct null. That's why I was so shocked when it worked with VB.NET without using Nullable. – hugoware Nov 19 '08 at 22:02
  • 1
    @HBoss: Using nullable, you can make ANYTHING null! Even an `Integer`. I was astonished by the accepted answer by **BFree**, and luckily it answered my question that led me to this post: How can you initialize a Structure with it's default value... ;) – awe Dec 11 '09 at 07:19
-1

Because a Structure is made up of possibly several different Types (not a single value Type, but a possible composite of several different types), to ask if it is "Nothing" would break the logic of the use of "Nothing". Nothing tests differently depending on the type that you're testing and therefore a complex type does not adhere to the logic of the use of "Nothing". However, for this type of testing, i.e., with a structure having all of its component members at their respective "Nothing" values, we use the function "IsNothing". For example:

Public Class Employees
    Public Structure EmployeeInfoType
       Dim Name As String    ' String
       Dim Age as Integer    ' Integer
       Dim Salary as Single  ' Single
    End Structure

    Private MyEmp as New EmployeeInfoType

    Public Function IsEmployeeNothing(Employee As EmployeeInfoType) As Boolean
       If **IsNothing**(Employee) Then
          Return True
       Else
          Return False
       End If
    End Function
End Class
piBoss
  • 1
  • 2