gpt4 book ai didi

vb.net - 使整数为空

转载 作者:行者123 更新时间:2023-12-03 15:16:27 24 4
gpt4 key购买 nike

我有一个更新函数,可以通过数据集更新 sql server db 表。表中的字段之一是整数并接受空值。因此,当我填充更新函数时,我需要一种在函数需要整数时输入空值的方法。

我试图这样做,但 _intDLocation = ""抛出异常

Dim _dLocation As String = udDefaultLocationTextEdit.Text
Dim _intDLocation As Integer
If _dLocation <> "" Then
_intDLocation = Integer.Parse(udDefaultLocationTextEdit.Text)
Else
'NEED HELP HERE
_intDLocation = ""
End If

最佳答案

整数不能设置为 Null。您必须通过在单词 Integer 后添加问号使整数“可空”。现在 _intDLocation 不再是一个普通的整数。它是 Nullable(Of Integer) 的一个实例.

Dim _dLocation As String = udDefaultLocationTextEdit.Text
Dim _intDLocation As Integer?
If _dLocation <> "" Then
_intDLocation = Integer.Parse(udDefaultLocationTextEdit.Text)
Else
_intDLocation = Nothing
End If

稍后,如果您想检查 null,您可以使用这个方便、易读的语法:
If _intDLocation.HasValue Then
DoSomething()
End If

在某些情况下,您需要将值作为实际整数而不是可以为空的整数来访问。对于这些情况,您只需访问
_intDLocation.Value

阅读有关 Nullable 的所有信息 here .

关于vb.net - 使整数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3628757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com