首页 > 解决方案 > IsNumeric 语句和范围

问题描述

大家好,我正在执行一项任务,我必须验证用户输入数据。当问我的导师我是否可以使用 IsNumeric 来验证两个数字之间的范围时,他只是说可以,并没有告诉我如何。现在我知道您可以使用 if statements 进行验证,并且我有几个使用基本的方法:

if hours < 0 then
   Messagebox("Please enter a value greater than 0" "Input Value to 
     low" messagebox.buttons retry/cancel) ''something like that
Elseif hours > 23 then
   Messagebox( "please enter a value less than 23"  "Input Value to 
   high" messagebox.buttons retry/cancel)
End if

我什至问他是否可以在 if 语句中使用 AND 来确定数据范围。再次是的,没有例子

我想到的例子

If hours < 0 AND hours > 23 then
 '' continue processing
Else
    Messagebox("Please enter a Value between 0 and 23" "Input 
     value 
 out of range" messagebox.buttons retry/cancel)
End if

标签: vb.netvisual-studio-2010

解决方案


针对您的示例,您可以尝试以下操作:

If isnumeric(hours) then
    If hours < 0 AND hours > 23 then
        '' continue processing
    Else
        MessageBox.Show("Input value out of range", 
                        "Please enter a Value between 0 and 23",
                        MessageBoxButtons.RetryCancel)
    End if
End if

推荐阅读