2014-11-14 82 views
-4
Dim dbBorrow As Double 
    dbBorrow = txtBorrow.Text 
    If dbBorrow < 500000 Then 
     MessageBox.Show("Lowest Amount which can be borrow is RS.500000") 
    ElseIf dbBorrow > 7000000 Then 
     MessageBox.Show("Maximum amount which can be borrowed is RS.7000000") 
    ElseIf dbBorrow = "" Then 

     MessageBox.Show("Pls Enter the amount you want to borrow") 

我的問題說,如果我想獲得貸款的val應介於500000和7000000之間,所以如果其小於500000,那麼我用一個消息框說,貸款不能獲得我使用相同的東西超過70000000.但如果文本框留空,我想顯示一條消息說,請輸入一個VAL。我試着出現這個錯誤出現「從字符串轉換」「鍵入'雙'是無效的。」需要幫助糾正這個VB.net

+0

'dbBorrow'是一個雙。 'txtBorrow.Text'是一個字符串。您需要使用'Double.TryParse'將文本框中的數字轉換爲實際的數字。從錯誤信息判斷,該文本框中沒有文本 – Plutonix 2014-11-14 16:10:33

+0

您需要首先切換選項嚴格 – 2014-11-14 16:47:46

+0

明顯重複[需要使用此代碼的VB.net](http://stackoverflow.com/questions/26933621/need -with-這個代碼-VB網) – 2014-11-14 18:29:00

回答

1

你正在混合字符串和雙。您應該啓用嚴格的選項,這將有助於您瞭解發生的情況。

如果您正確地將您的字符串轉換爲雙精度型,您可以將雙精度型設置爲NaN,如果它不是一個有效的數字(這不僅適用於空字符串,還適用於書寫字母)。

Dim dbBorrow As Double 

If Not Double.TryParse(txtBorrow.Text, dbBorrow) Then 
    dbBorrow = Double.NaN 
End If 

If dbBorrow < 500000 Then 
    MessageBox.Show("Lowest Amount which can be borrow is RS.500000") 
ElseIf dbBorrow > 7000000 Then 
    MessageBox.Show("Maximum amount which can be borrowed is RS.7000000") 
ElseIf Double.IsNaN(dbBorrow) Then 
    MessageBox.Show("Pls Enter the amount you want to borrow") 
Else 
    ' ... 
End If 
0

我做了一些調整,以您的代碼,請參見下面...使用的TryParse不會拋出一個異常時,不是雙,它將返回false ...

Dim dbBorrow As Double = 0 
If Double.TryParse(txtBorrow.Text, dbBorrow) Then 
    If dbBorrow < 500000 Then 
    MessageBox.Show("Lowest Amount which can be borrow is RS.500000") 
    ElseIf dbBorrow > 7000000 Then 
    MessageBox.Show("Maximum amount which can be borrowed is RS.7000000") 
    End If  
Else 
    MessageBox.Show("Pls Enter the amount you want to borrow") 
End If