我在使用 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 Nothing
或 If Not _TicketCharge.HasValue
关于vb.net - 为什么 VB.net 类中的可空公共(public)属性不接受 0(零)作为赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/760675/