2014-10-07 63 views
0
Public Class Form1  
    Private Sub btn_calculate_Click(sender As Object, e As EventArgs) Handles btn_calculate.Click 
     Dim MonthPayment As Double 
     Dim LoanAmount As Double = txtbox_loan.Text 
     Dim IntRate As Double = txtbox_IntRate.Text 
     Dim Years As Double = txtbox_years.Text 
     Dim tempI, temp1, temp2 As Double 'temporary variables' 
     tempI = IntRate/1200 
     temp1 = (1 + tempI)^((-12) * Years) 
     temp2 = (tempI/(1 - temp1)) 
     MonthPayment = temp2 * LoanAmount 
     txtbox_MonthPayment.Text = "$" & Math.Round(MonthPayment, 2) 
    End Sub 
End Class 

這是迄今爲止我所擁有的。我不斷收到錯誤A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll,它突出顯示LoanAmount As Double = txtbox_loan.Text,我想我需要改變它是一個字符串,但不知道如何。有什麼建議麼?Visual Basic 2012「在Microsoft.VisualBasic.dll中發生類型'System.InvalidCastException'的第一次機會異常」

回答

1

通常,您不能將字符串分配給雙變量。爲了與舊的VB6代碼兼容,Visual Studio提供了在項目屬性中關閉此行爲設置Option Strict Off的選項。

我強烈建議保留Option Strict設置爲On,因爲這些類型的分配在由Option Strict Off啓用的自動轉換處理時可能非常危險。

再回到你的代碼,你需要使用double.TryParse和寫入

Dim LoanAmount As Double 
if Not double.TryParse(txtbox_loan.Text, LoanAmout) then 
    .... Message for your user ....conversion not possible.. Not a valid double number ??? 
End If 

等了,你需要轉換

其他數值
相關問題