检查一个字符串变量是否具有整数值

21

我正在开发一个项目,让孩子们向圣诞老人发送消息。不幸的是,如果他们在年龄字段中输入字符串而不是整数,程序就会崩溃并返回“从字符串“[exampleString]”转换为类型'Double'无效”的错误信息。 有没有办法检查他们是否输入了整数?以下是代码。

If childAge > 0 And childAge < 150 Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

感谢您,Kai :)
9个回答

48

一个非常简单的技巧是尝试解析字符串为整数。如果成功了,那它就是一个整数(惊不惊喜)。

Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
    ' childAge successfully parsed as Integer
Else
    ' childAge is not an Integer
End If

9

补充Styxxy的回答,如果您不需要结果,只需将其替换为vbNull:

If Integer.TryParse(childAge, vbNull) Then

4

你可以进行以下两个测试来确保你所获取的输入是一个整数:

If IsNumeric(childAge) AndAlso (InStr(1, childAge, ".") <> 0) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
    If childAge < 0 OrElse childAge > 150 Then
        fmSecA2 = "I don't believe it's possible to be" & childAge & " years old..."
    End If
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"

InStr函数查找字符串,如果没有找到,返回零。因此,在将此测试与IsNumeric结合使用时,还要排除输入了某些浮点数据类型的可能性。


1
这里假设“.”作为小数点分隔符——在大多数情况下可能是可以的。但最好使用Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator。 - Poat

3

IsNumeric是VB内置的函数,它将返回一个true/false值。

If IsNumeric(childAge) AndAlso (childAge > 0 And childAge < 150) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

谢谢,这非常有帮助! - Kai
如果孩子输入数字和字母,这仍然会导致错误。 - Dman

1

您可以使用这个。

Sub checkInt() 
    If IsNumeric(Range("A1")) And Not IsEmpty(Range("A1")) Then 

        If Round(Range("A1"), 0) / 1 = Range("A1") Then 
            MsgBox "Integer: " & Range("A1") 
        Else 
            MsgBox "Not Integer: " & Range("A1") 
        End If 
    Else 
        MsgBox "Not numeric or empty" 
    End If 
End Sub 

有点混乱,但没关系。谢谢你的帮助! :) - Kai

0
Dim Input 

 Input = TextBox1.Text
 If Input > 0 Then 
   ............................
   ............................
 Else 
   TextBox2.Text = "Please only enter positive integers"
 End If

2
虽然这段代码片段可能解决了问题,但包括解释真的有助于提高您的帖子质量。请记住,您正在为未来的读者回答问题,而这些人可能不知道您的代码建议原因。 - secelite

0
从Styxxy的答案开始,如果你将其解析为字节而不是整数,那么它也可以一次检查负年龄和最大年龄255。
Dim childAgeAsByte As Byte
If Byte.TryParse(childAge, childAgeAsByte) Then
    ' childAge successfully parsed as Byte
Else
    ' childAge is not a Byte
End If

克里斯蒂安


0
Try
    If TextBox1.Text > 0 Then
        Label1.Text = "Integer"
    End If
Catch ex As Exception
    Label1.Text = "String"
End Try

通过这个,你可以把任何东西放进TextBox1,如果你放文本,那么Label1就是字符串,如果你放数字,那么就是整数


-1
在 .Net 中,您可以使用 GetType() 来确定变量的数据类型。
Dim n1 As Integer = 12
Dim n2 As Integer = 82
Dim n3 As Long = 12

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()))
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()))
' The example displays the following output: 
'       n1 and n2 are the same type: True 
'       n1 and n3 are the same type: False  

基于上述示例,您可以编写以下代码片段:
If childAge.GetType() = "Integer" then '-- also use childAge.GetType().Name = "Int32"
  ' do something
End if

2
那怎么回答问题呢? - Meta-Knight
2
@Meta-Knight,我只是展示了一段带有GetType()的示例代码,以查找变量类型。 OP可以使用它来定义它是“integer”,“long”,“boolean”等类型。唉,你向我投了反对票:( - bonCodigo
4
这个问题是关于确定一个字符串是否能够成功地解析为整数。使用GetType在这里没有用,因为它的类型仍然是字符串。 - Jason Tyler
@JasonTyler 标题是“VB - 检查变量是否为整数”。 - bonCodigo
1
@Meta-Knight,这里还有两个检查整数的答案!所以当我提供一种正确的检查“类型”的方法时,对我的回答进行投票不是很公平。将字符串作为整数传递是首要的,但检查“类型”也在问题范围内。 - bonCodigo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接