VB.NET Structs and Nothing - Problems

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 ?

+4
source share
4 answers

Structure is a type of meaning; it cannot be anything. The Nullable type can solve your problem by putting a question mark after the type name to make it short and fast. Here is an example:

 Module Module1 Structure Test Dim field1 As String End Structure Private Function Foo() As Test? Return Nothing End Function Sub Main() Dim st As Test? = Foo() Debug.Assert(st is Nothing) End Sub End Module 
+13
source

The first two following methods are often used in the .NET Framework. The method you use will depend on your requirements. I will use method number 3 if the structure is tested infrequently. For frequent testing, I will use method No. 1 if method No. 2 is not suitable for "empty" testing. For example, the Point structure defines an empty point as {x = 0, y = 0}, which, in my opinion, is incorrect. Therefore, I would use the first method in implementing my point structure.

Method 1: Defining an Empty Test to Test Comparison

Add a generic Test to use for an empty comparison.

 Structure Test Public Shared Empty As Test = New Test Dim field1 As String End Structure 

Check how:

 If st = Test.Empty Then 

Method 2: Define IsEmpty Property for Testing

Define the IsEmpty property based on the internal state of the structure.

 Structure Test Public ReadOnly Property IsEmpty As Boolean Get Return Len(field1) = 0 End Get End Property Dim field1 As String End Structure 

Check how:

 If st.IsEmpty Then 

Method 3: Use Nullable (Of T)

Define as:

 Dim st As Test? = Foo() '--or-- Dim st As Nullable(Of Test) = Foo() 

Check how:

 If st Is Nothing Then '--or-- If st.HasValue = False Then 

Note

I have not tested the above code, and I do not have access to my code library.

Inspiration

Take a look at the Microsoft Point and Color structures using their published source code or using the .NET Reflector.

+8
source

There is no such thing as assigning "Nothing to a structure, not its members."

It sounds very similar to the fact that you should look at value types with a null value and Nullable(Of T) - if you need to imagine the absence of a β€œreal” value for a value type, then this is the reason it was invented.

Consider, for example, Byte . A Byte value can have any of 256 values ​​(0-255). If you assign it a value of Nothing , which will actually make it 0. It cannot make it "some value not in the range 0-255", because it will be stored as a byte. I find it somewhat annoying that VB really allows you to use Nothing here, actually ... because philosophically the β€œmissing” value and the value 0 really differ from each other.

In any case, types with a null value wrap the "normal" value types and provide an additional boolean to tell if a useful value is really there.

0
source

but, nevertheless, I cannot check if there is st Nothing:

 If st Is Nothing Then 

To check if a type with a null value has a value, use st.HasValue .

0
source

Source: https://habr.com/ru/post/1314581/


All Articles