Why won't the Nullable Public Property in the VB.net class accept 0 (zero) as the destination?

I have a public property with a null value for a class using Vb.net 3.5:

    Public Property TicketCharge() As Nullable(Of Decimal)
        Get
            If _TicketCharge = Nothing Then
                Return Nothing
            Else
                Return _TicketCharge
            End If
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            If value.HasValue Then _TicketCharge = value
        End Set
    End Property

There is a method that assigns a value. It works fine, except when I try to set it to 0 (zero).

    If FundBuySell = "Exchange $" Or FundBuySell = "Exchange Shares" Then
            TicketCharge = 0
    Else

When I assign zero and then retrieve it, it shows property = nothing.

I need the property to sometimes be Null and Zero. Can someone explain what is happening?

+3
source share
4 answers

If _TicketCharge = Nothingdoesn't do what you expect (inflammatory comment: as always with VB.NET, sorry guys couldn’t resist). You either need If _TicketCharge Is NothingorIf Not _TicketCharge.HasValue

+2

, _TicketChange, , . VB.Net, , . Nothing Nullable .

_TicketChange.HasValue.

+1

, :

If Not IsDBNull(var) 
+1

:

VB.Net "" , " " . "=", "Is" "HasValue", , Nullable ( , !). 0, Get True.

0

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


All Articles