我正在做一个允许孩子们向圣诞老人发送消息的项目。不幸的是,如果他们在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


谢谢,
启:)

最佳答案

一个非常简单的技巧是将字符串try parse作为整数。如果成功,则为整数(惊喜)。

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

关于vb.net - 检查字符串变量是否具有整数值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13980538/

10-09 04:39