20

我正在做一个允许孩子们向圣诞老人发送信息的项目。不幸的是,如果他们在 AGE 字段中输入字符串而不是整数,程序会崩溃并返回从字符串“[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

谢谢,凯:)

4

9 回答 9

47

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

Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
    ' childAge successfully parsed as Integer
Else
    ' childAge is not an Integer
End If
于 2012-12-20T21:18:38.880 回答
7

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

If Integer.TryParse(childAge, vbNull) Then
于 2018-10-30T14:09:50.000 回答
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 组合时,您还排除了输入某些浮点数据类型的可能性。

于 2012-12-21T06:07:02.463 回答
3

IsNumeric 内置在 VB 中,将返回真/假

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
于 2012-12-20T21:18:51.933 回答
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 
于 2012-12-20T21:16:11.253 回答
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

克里斯蒂安

于 2014-12-10T10:36:26.207 回答
0
Dim Input 

 Input = TextBox1.Text
 If Input > 0 Then 
   ............................
   ............................
 Else 
   TextBox2.Text = "Please only enter positive integers"
 End If
于 2016-12-14T10:53:59.137 回答
0
Try
    If TextBox1.Text > 0 Then
        Label1.Text = "Integer"
    End If
Catch ex As Exception
    Label1.Text = "String"
End Try

有了这个你可以放任何东西TextBox1,如果你放文本,那么你得到Label1的是字符串,如果你放数字,那么你得到的是整数

于 2020-07-08T08:58:03.017 回答
-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
于 2012-12-20T21:18:30.377 回答