Vb.net 检查字符串变量是否具有整数值

Vb.net 检查字符串变量是否具有整数值,vb.net,string,visual-studio-2010,parsing,integer,Vb.net,String,Visual Studio 2010,Parsing,Integer,我正在做一个让孩子们给圣诞老人发信息的项目。不幸的是,如果他们在年龄字段中输入字符串而不是整数,程序将崩溃并返回从字符串“[exampleString]”到类型“Double”的转换无效。 有没有办法检查他们是否输入了整数?这是代码 If childAge > 0 And childAge < 150 Then fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing

我正在做一个让孩子们给圣诞老人发信息的项目。不幸的是,如果他们在年龄字段中输入字符串而不是整数,程序将崩溃并返回从字符串“[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
如果儿童年龄>0且儿童年龄<150,则
fmSecA2=“哇!你已经”&childAge&“岁了?你正在成长为一个大”&childGender&“现在!”
其他的
fmSecA2=“呃,我真的不明白你的年龄。这是你编造的吗?呵呵!”
如果结束
谢谢, Kai:)

你可以用这个

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 

在.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
    • 一个非常简单的技巧是将字符串转换为整数。如果成功,则为整数(惊喜)


      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
      
      如果是数字(childAge)且也是数字(childAge>0且childAge<150),则
      fmSecA2=“哇!你已经”&childAge&“岁了?你正在成长为一个大”&childGender&“现在!”
      其他的
      fmSecA2=“呃,我真的不明白你的年龄。这是你编造的吗?呵呵!”
      如果结束
      
      您可以执行以下两个测试,以合理确定您得到的输入是整数:

      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!"
      
      如果是数字(childAge)且也是数字(InStr(1,childAge,“.”)0),则
      fmSecA2=“哇!你已经”&childAge&“岁了?你正在成长为一个大”&childGender&“现在!”
      如果儿童年龄<0或儿童年龄>150,则
      fmSecA2=“我不相信这是可能的”&儿童年龄&“岁…”
      如果结束
      其他的
      fmSecA2=“呃,我真的不明白你的年龄。这是你编造的吗?呵呵!”
      

      如果函数找不到正在查找的字符串,则返回零,因此当将该测试与IsNumeric组合时,您还排除了输入某个浮点数据类型的可能性。

      从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
      

      Kristian

      补充Styxxy的回答,如果你不需要结果,就用vbNull替换它:

      Dim Input 
      
       Input = TextBox1.Text
       If Input > 0 Then 
         ............................
         ............................
       Else 
         TextBox2.Text = "Please only enter positive integers"
       End If
      
      If Integer.TryParse(childAge, vbNull) Then
      

      有了这个,你可以在
      TextBox1
      中放入任何东西,如果放入文本,那么得到的
      Label1
      是字符串,如果放入数字,那么得到的是整数,这如何回答这个问题呢?@Meta Knight我只是用
      GetType()
      显示一个示例代码来找出变量类型。OP可以使用它来定义它是否是
      整数
      布尔
      等类型。叹息你否决了我的投票:(这个问题是关于确定一个字符串是否可以成功解析为整数。在这里使用GetType没有帮助。它应该是String类型。@JasonTyler标题是“VB-检查一个变量是否是整数”@Meta Knight这里还有两个关于检查整数的答案!因此,在我提供检查类型的正确方法时,对我的答案进行否决是不公平的。将字符串作为整数传递是主要的,但是检查类型也在问题中。谢谢!这真的很有帮助!:)谢谢,这非常有帮助!如果孩子输入一个数字和字母,这仍然会让人有点困惑,但没关系。谢谢你的帮助!:)虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来为读者回答这个问题,而这些人可能不知道您的代码建议的原因。这假定“.”是一个十进制分隔符,在大多数情况下这可能是正确的。但最好使用Globalization.CultureInfo.CurrentCulture.NumberFormat.numberdecimalseparat来代替
      If Integer.TryParse(childAge, vbNull) Then
      
      Try
          If TextBox1.Text > 0 Then
              Label1.Text = "Integer"
          End If
      Catch ex As Exception
          Label1.Text = "String"
      End Try