I'm having headaches using structures and functions that return Nothing to VB.NET.
Let me explain this code here:
Public Class Form1 Structure Test Dim field1 As String End Structure Private Function Foo() As Test Return Nothing End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim st As Test = Foo() End Sub End Class
In the previous code, when I return Nothing as the result of the Foo function, I would expect st Nothing . But that is not what is happening.
Then I found in the MSDN documentation:
Assigning a variable does not set a default value for the declared type. If this type contains variables, they all have default values.
So, I found that when I assign a Nothing structure to a structure, all its members are set by default, not the structure itself.
In addition, I tried to make the st type Nullable by declaring:
Dim st As Nullable(Of Test) = Foo()
but, nevertheless, I cannot check if there is st Nothing:
If st Is Nothing Then
or
If st.Equals(Nothing) Then
So the questions are:
1 - Is it possible to assign Nothing for a structure, and not its members?
2 - How to check if the value of the returned structure is Nothing ?