vb.net - 为什么 VB.net 类中的可空公共(public)属性不接受 0(零)作为赋值?

标签 vb.net nullable

我在使用 Vb.net 3.5 的类上有一个可为 null 的公共(public)属性:

    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

有一个分配值的方法。它工作正常,除了当我尝试为其分配值 0(零)时除外。

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

当我分配零然后检索它时,它显示该属性=无。

我需要该属性有时为空,有时为零。有人可以解释一下发生了什么事吗?

最佳答案

If _TicketCharge = Nothing 没有按照您的预期进行操作(煽动性评论:VB.NET 总是这样;抱歉,大家无法抗拒)。您需要 If _TicketCharge Is NothingIf Not _TicketCharge.HasValue

关于vb.net - 为什么 VB.net 类中的可空公共(public)属性不接受 0(零)作为赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/760675/

相关文章:

json - 在 Wonderware 的 ArchestrA/Quickscript.NET 中反序列化 JSON

c++ - VB换行转义字符?

vb.net - VB.NET中有条件三元运算符吗?

c# - ASP.NET Web 应用程序的推荐体系结构是什么?

.net - 何时在 F# 中引发 nullArg

c# - 了解空合并运算符 (??)

c# - Nullable<T> 的内存占用是多少

c# - 你能定义一个接受*任何*可为空的类型、值或引用的泛型吗?

vb.net - VB.NET 中的正态分布随机数

java - 是否需要覆盖注解 - Kotlin