VB检查int是否为空 [英] VB Check if int is empty

查看:288
本文介绍了VB检查int是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常无聊的问题,对不起,但我真的不知道呢;)我一直尝试使用string.empty,但是使用小数会产生错误。



有什么功能吗?不幸的是,对于最简单的问题,谷歌没有答案

解决方案

你的标题(和标签)询问int ,但你的问题是你得到一个带有十进制的错误。无论哪种方式,当涉及值类型 (例如整数十进制等)。它们不能设置为 Nothing ,因为你可以使用引用类型(如 String 或类)。相反,值类型具有隐式默认构造函数,可自动将该类型的变量初始化为其默认值。对于数值,例如 Integer 十进制,这是0.对于其他类型,请参阅此表



因此,您可以使用以下代码检查值类型是否已初始化:



'pre> 昏暗myFavoriteNumber为整数= 24
。如果myFavoriteNumber = 0然后
'' #此代码显然会永远不会运行,因为该值是设置为24
结束如果

尺寸mySecondFavoriteNumber作为整数
。如果mySecondFavoriteNumber = 0,那么
MessageBox.Show( 你没有指定了第二个最喜欢的数字! )
结束如果

请注意 mySecondFavoriteNumber 自动初始化为0(整数的默认值)behin d编译器的场景,所以 If 语句是 True 。事实上,上面的 mySecondFavoriteNumber 的声明等同于以下声明:

 将mySecondFavoriteNumber调整为整数= 0 



当然,就像你'我们可能已经注意到,没有办法知道一个人最喜欢的号码是实际 0,还是他们还没有指定一个喜欢的号码。 如果您确实需要一个可以设置为 Nothing 的值类型,则可以使用 Nullable(Of T) ,将变量声明为:

 昏暗mySecondFavoriteNumber为可为空(整数)



'p>并检查以查看它是否已经被分配如下:

 如果mySecondFavoriteNumber.HasValue然后
'' #A值已指定,因此在消息框中
MessageBox.Show显示它( 你最喜欢的号码是:与 & mySecondFavoriteNumber.Value)
,否则
' '#No值已被指定,因此Value属性为空
MessageBox.Show(你还没有指定第二个最喜欢的号码!)
结束如果


A really boring question, sorry, but I really don't know that yet ;) I've tried always string.empty, but with a decimal this produces an error.

Is there any function? Unfortunately, for the simplest questions, there are no answers on google

解决方案

Your title (and tag) asks about an "int", but your question says that you're getting an error with a "decimal". Either way, there is no such thing as "empty" when it comes to a value type (such as an Integer, Decimal, etc.). They cannot be set to Nothing as you could with a reference type (like a String or class). Instead, value types have an implicit default constructor that automatically initializes your variables of that type to its default value. For numeric values like Integer and Decimal, this is 0. For other types, see this table.

So you can check to see if a value type has been initialized with the following code:

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

Note that mySecondFavoriteNumber is automatically initialized to 0 (the default value for an Integer) behind the scenes by the compiler, so the If statement is True. In fact, the declaration of mySecondFavoriteNumber above is equivalent to the following statement:

Dim mySecondFavoriteNumber as Integer = 0


Of course, as you've probably noticed, there's no way to know whether a person's favorite number is actually 0, or if they just haven't specified a favorite number yet. If you truly need a value type that can be set to Nothing, you could use Nullable(Of T), declaring the variable instead as:

Dim mySecondFavoriteNumber as Nullable(Of Integer)

And checking to see if it has been assigned as follows:

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

这篇关于VB检查int是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆