Unable to see structure members declared as NULL

In VB.NET, why can't I see a member of the structure when I make it a null type?

Example:

Public Structure myNullable  
    Dim myNullVar As Integer  
End Structure

Sub Main()  
    Dim myInstance As myNullable 'This works.  
    Dim myNullableInstance? As myNullable 'This works.   
    myInstance.myNullVar = 1  'This works.     
    myNullableInstance.myNullVar = 1   'This doesn't work.  
End Sub
+3
source share
6 answers

As others have said, you need to use a property Valueto retrieve the value. However, it System.Nullable<T>is immutable - the property Valueis read-only. It will return a copy of the value, so even if you can change the field, it will not do what you want.

- . - , Nullable<T> , . .

, . , NULL, .

+3

, NULL .Value. myNullableInstance.Value.myNullVar = 1, .

- , . , . , Value Nullable, , Value is ReadOnly.

:

Dim myInstance as myNullable
myInstance.myNullVar = 1
myNullableInstance = myInstance
+1

Nullable<T> Value. :

myNullableInstance.Value.myNullVar = 1
0

, :

myNullableInstance.Value.myNullVar = 1

, , Value.

, . , , , . , Nullable, .

0

- , , Microsoft . .

, , -. Microsoft (), , . , , , .

, . , . ( !)

, Nullable , NULL.

0

When a type is NULL, you should check to see if it has a value, and then access that value only if it is:

if(myNullableInstance.hasValue)
myNullableInstance.value.myNullVar=1
-1
source

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


All Articles