非常抱歉,这是一个非常无聊的问题,但我还不知道如何解决;) 我尝试了 always string.empty,但是使用十进制数会产生错误。
是否有任何函数可用?很遗憾,对于最简单的问题,谷歌上没有答案。
(Note: I kept the original HTML tags and only provided the translation.)
非常抱歉,这是一个非常无聊的问题,但我还不知道如何解决;) 我尝试了 always string.empty,但是使用十进制数会产生错误。
是否有任何函数可用?很遗憾,对于最简单的问题,谷歌上没有答案。
Integer
、Decimal
等),都不存在所谓的 "空"。它们不能像 引用类型(如 String
或类)那样被设置为 Nothing
。相反,值类型有一个隐式默认构造函数,它会自动将该类型的变量初始化为其默认值。对于像 Integer
和 Decimal
这样的数值,这个默认值是0。对于其他类型,请参阅 此表。因此,你可以使用以下代码来检查值类型是否已经初始化:Dim myFavoriteNumber as Integer = 24
If myFavoriteNumber = 0 Then
''#This code will obviously never run, because the value was set to 24
End If
Dim mySecondFavoriteNumber as Integer
If mySecondFavoriteNumber = 0 Then
MessageBox.Show("You haven't specified a second favorite number!")
End If
mySecondFavoriteNumber
初始化为0(Integer
的默认值),因此If
语句为True
。实际上,上面mySecondFavoriteNumber
的声明等同于以下语句:Dim mySecondFavoriteNumber as Integer = 0
Nothing
的值类型,你可以使用Nullable(Of T)
,将变量声明为:Dim mySecondFavoriteNumber as Nullable(Of Integer)
检查是否已分配如下:
If mySecondFavoriteNumber.HasValue Then
''#A value has been specified, so display it in a message box
MessageBox.Show("Your favorite number is: " & mySecondFavoriteNumber.Value)
Else
''#No value has been specified, so the Value property is empty
MessageBox.Show("You haven't specified a second favorite number!")
End If
Dim foo As Nullable(Of Integer) = 1
Dim bar As Nullable(Of Decimal) = 2
If foo = 1 Then
If bar = 2 Then
foo = Nothing
bar = Nothing
If foo Is Nothing AndAlso bar Is Nothing Then Stop
End If
End If
嗯,数字的默认值为0,但您也可以尝试这个:
int x = 123;
String s = "" + x;
然后检查长度或字符串's'是否为空。
Dim mySecondFavoriteNumber as Integer?
与Dim mySecondFavoriteNumber as Nullable(Of Integer)
是相同的。 - Mark Schultheiss