2016-11-13 110 views
0

我在入門級視覺基礎類。我正在嘗試使用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 
+0

如果您使用'Double.TryParse()',則不需要測試**和**轉換 - 它將執行這兩個操作 – Plutonix

回答

0

哦,我想通了。當我應該測試txtVariable.text時,我正在測試dblVariable。捂臉。

+0

這就是那些老式的VB函數的問題 - 它們幾乎全部採用Object對象參數所以你可以通過任何事情,並犯這樣的錯誤 – Plutonix

+0

感謝所有的信息@plutonix,我真的appriciate它。我們被教導使用CSng,CInt等等。雖然這樣的聲音被破壞了(正如我的教授在大多數方面)。 –

+0

它/它們不會被官方棄用*只是通過普遍的共識,各種解析方法更健壯一些,如果您習慣於不使用遺留方法,切換到c#會更容易。 – Plutonix

0

假設你有興趣的教育,不只是一個檔次的,這裏是一個使用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方法的類型更強,所以只能傳遞一個字符串。