In VB.NET, why can’t I see a member of a structure when I make it a nullable 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
As others have said, you need to use the
Valueproperty to fetch the value. However,System.Nullable<T>is immutable – theValueproperty is read-only. It will be returning a copy of the value, so even if you could change the field, it wouldn’t do what you want.This is actually a good thing – value types should be immutable. Mutable structs are horrible – if the fact that
Nullable<T>makes it hard to use your type pushes you down the immutability route, that’s great.Make your field read-only, and add a constructor to let you pass in the value. Then instead of trying to modify the nullable value, assign an entirely new value to the variable.