[英]What can I use instead of isnumeric in vb.net
I am writing a simple Random Number guessing game and am using the statement isnumeric. 我正在写一个简单的随机数猜谜游戏,并且正在使用语句isnumeric。 This is a Visual Basic program being done in Visual Studio 2013. 这是在Visual Studio 2013中完成的Visual Basic程序。
Here is my code: 这是我的代码:
Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click
Dim intTry As Integer
Dim strInputBox As String
strInputBox = InputBox("Enter Your Number to Guess.", "Input Needed")
If IsNumeric(strInputBox) Then
intTry = CInt(strInputBox)
lstTried.Items.Add(intTry)
Else
MessageBox.Show("Please Type Only Numbers")
End If
If intTry = intRandomNumber Then
MessageBox.Show("You Got It!", "Correct Guess")
Else
MessageBox.Show("Incorrect. Please try again.", "Incorrect Guess")
End If
I'd like to use something in place of "IsNumeric" in my If statement. 我想在If语句中使用某些内容代替“ IsNumeric”。 I'm not sure at all how to do that. 我完全不确定该怎么做。 Can someone help me. 有人能帮我吗。 I tried to use integer.tryparse but could not make it work. 我尝试使用integer.tryparse,但无法正常工作。 Specific help would be appreciated. 具体的帮助将不胜感激。 This works right now, I'm tempted to leave it alone, but I was told it's old style code and there is another way to do it. 现在可以正常工作,我很想将其保留,但是有人告诉我它是旧样式的代码,还有另一种方法可以做到。
Thanks, 谢谢,
Steve 史蒂夫
You can try this, I also made some suggestive corrections to your code. 您可以尝试一下,我还对您的代码做了一些建议性的更正。
Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click
Dim intTry As Integer = 0
'This wont throw an exception if it's not an integer, it will come back false...
If Integer.TryParse(InputBox("Enter your number to guess.", "Input Needed"), intTry) Then
lstTried.Items.Add(intTry)
Else
MessageBox.Show("Please Type Only Numbers")
Exit Sub
End If
If intTry = intRandomNumber Then
MessageBox.Show("You Got It!", "Correct Guess")
Else
MessageBox.Show("Incorrect. Please try again.", "Incorrect Guess")
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.