【问题标题】:My conditional statement isn't working我的条件语句不起作用
【发布时间】:2016-11-13 20:47:01
【问题描述】:

我正在上入门级视觉基础课程。我正在尝试使用 IsNumeric 布尔函数测试用户输入以查看它是否为数字。基本上,如果输入不是数字,我希望它抛出一个消息框,如果它是数字,我希望它将数值设置为变量。每次输入非数字值时,我都不会收到消息框,而是在尝试使用 CDbl 将非数字字符串转换为双精度时出现异常。我做错了什么?

    If txtInches.Text = "" Or txtFeet.Text = "" Then 'Checks to ensure height fields were not left blank
        'If fields were blank, throws error message up, changes textbox backgroud color to red, focuses user on error and exits sub.
        MessageBox.Show("You must enter a value for both feet and inches. Don't leave these fields blank, and try again.", "Height Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtInches.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf txtAge.Text = "" Then 'Checks to see if age field was blank
        'If age field was blank, throws error message up, changes textbox backgroud color to red, focuses user on error and exits sub.
        MessageBox.Show("You must enter your age. Don't leave this field blank, and try again.", "Age Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtInches.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblFeet) Then
        'If feet input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Feet must be a numeric value. Please try again.", "Height Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblInches) Then 'Checks to see if height input is numeric
        'If inches input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Inches must be a numeric value. Please try again.", "Height Error", MessageBoxButtons.OK)
        txtInches.BackColor = Color.Red
        txtInches.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblAge) Then 'Checks to see if age input is numeric
        'If age input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Your age must be a number. Please try again.", "Age Error", MessageBoxButtons.OK)
        txtAge.BackColor = Color.Red
        txtAge.Focus()
        Exit Sub
    Else
        dblFeet = CDbl(txtFeet.Text)
        dblInches = CDbl(txtInches.Text)
        dblAge = CDbl(txtAge.Text)
    End If

【问题讨论】:

  • 如果您使用Double.TryParse(),则无需测试转换-两者都可以

标签: vb.net if-statement


【解决方案1】:

哦,我想通了。当我应该测试 txtVariable.text 时,我正在测试 dblVariable。掌心。

【讨论】:

  • 这就是那些老式 VB 函数的问题——它们几乎都接受 Object 参数参数,所以你可以传递任何东西并犯这样的错误
  • 感谢@plutonix 提供的所有信息,我真的很感激。我们被教导使用 CSng、CInt 等。不过,这听起来像是被贬低了(就像我的教授在大多数方面一样)。
  • It/它们并没有被 官方 弃用,只是出于普遍的共识,各种 Parse 方法更健壮一些,如果您养成不使用旧方法的习惯,切换到 c# 更容易。
【解决方案2】:

假设您对教育感兴趣,而不仅仅是一个年级,这里是使用 NET 方法的更简洁的方法:

' assumes all the values are form/class 
' leval variables
Private nAge As Int32

然后进行验证:

    ' if it parses, then nAge will have the value
    If Integer.TryParse(tbAge.Text, nAge) = False Then
        MessageBox.Show("Please enter a valid integer for Age", 
                          "Annoying MsgBox", MessageBoxButtons.OK)
        Return
    End If
  • 我不知道你为什么要使用双打 - 你真的希望 "5.8" 英尺或输入 "28.7" 年龄?
  • 使用Integer.TryParse,您只能执行一项测试:它将检测/失败空字符串以及"I like pie"
  • 而不是一次告诉他们一个错误,您可以累积坏元素并告诉他们所有错误。 ErrorProvider 对此很有用。

旧 VB 函数的一个问题是几乎所有东西都是As Object。这使您可以执行IsNumeric(dblAge) 之类的操作,这将始终是正确的。 NET 方法的类型更强,因此您只能传递一个字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 2011-01-08
    • 2011-12-04
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多