2016-10-12 5 views
0

私の教授は、私たちにサブ手続きを使って住宅ローン電卓を作成するよう要求しています。私はサブのやByVal/ByRefを使わずに、このボタンをクリックするだけでこのタスクを完了できました。しかし、私はそれをいくつかの異なる関数とサブミットに分割すると、私はボタンをクリックして計算するとエラーが0になりますが、3つの表示出力はすべて0です。私はそれが私の関数calcブロックまたは私のbtnCalculate_Click Sub。VBモーゲージ・カルキュレータが正しい出力を表示しない

Public Class frmMortgage 

    Dim annualRateOfInterest, monthlyPayment, begBalance As Double 'Declaring variables 
    Dim intForMonth, redOfPrincipal, endBalance As Double 

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 

     Call getInput(annualRateOfInterest, monthlyPayment, begBalance) 

     Call displayOutput(intForMonth, redOfPrincipal, endBalance) 

    End Sub 

    Sub getInput(ByVal annualRateOfInterests As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double) 'Values input into txt boxes 
     annualRateOfInterest = CDbl(txtInterest.Text)/100 'Change number entered into a percentage 
     monthlyPayment = CDbl(txtPayment.Text) 
     begBalance = CDbl(txtBalance.Text) 
    End Sub 

    Function calc(ByVal annualRateOfInterest As Double, ByVal monthlyPayment As Double, ByVal begBalanceintForMonth As Double, ByVal redOfPrincipal As Double, ByVal endBalance As Double) 
     Return intForMonth = (annualRateOfInterest/12) * begBalance 'Take percentage divided by 12 times beginning balance 
     Return redOfPrincipal = monthlyPayment - intForMonth 
     Return endBalance = begBalance - redOfPrincipal 
    End Function 

    Sub displayOutput(ByRef intForMonth As Double, ByRef redOfPrincipal As Double, ByRef endBalance As Double) 
     mtbMonth.Text = CDbl(intForMonth) 'Display the calculated data into correct textboxes. 
     mtbROP.Text = CDbl(redOfPrincipal) 
     mtbEndBalance.Text = CDbl(endBalance) 
    End Sub 

End Class 
+0

あなたは 'calc'関数を全く使用しませんでした。もう一つは、 'Call'を使うのはちょっと冗長です。それを使わずに関数/ subsを呼び出すことができます。 –

+0

私はどのようにそれを私のクリックサブに追加するのですか?私はそれを呼び出すか参照しようとするとエラーが発生し、プログラムは実行されません。 – Devin

+0

関数は、** one **値のみを返すと予想されます。あなたのコードを確認してください。あなたは3を返しています。その場合、最初に返される 'intForMonth'が返されます。そしてあなたが得ているエラーは何ですか? –

答えて

0

私はただ1つをSubにまとめました。

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click 
    ComputeMortgage(Val(txtInterest.Text), Val(txtPayment.Text), Val(txtBalance.Text)) 
End Sub 

Sub ComputeMortgage(ByVal Interest As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double) 
    annualRateOfInterest = Interest/100 

    'COMPUTATION 
    intForMonth = (annualRateOfInterest/12) * begBalance 'Take percentage divided by 12 times beginning balance 
    redOfPrincipal = monthlyPayment - intForMonth 
    endBalance = begBalance - redOfPrincipal 

    'DISPLAYING 
    mtbMonth.Text = intForMonth 'Display the calculated data into correct textboxes. 
    mtbROP.Text = redOfPrincipal 
    mtbEndBalance.Text = endBalance 

End Sub 
+0

キャスティングが不要なキャスティングがたくさんあります...冗長です – Codexer

+0

@Zaggler、edited。それを指摘してくれてありがとう。 –

+0

ありがとう!それは今起こっていることをすべて理解するために今比較して比較しようとするとすばらしいです。 Valはどのように働きかけますか? – Devin

関連する問題