2013-01-13 24 views
-1

我想用一個函數做一個英鎊轉換爲歐元轉換器。 我希望系統做到這一點,當您在文本框中輸入數字並按下convert時,我希望標籤中的歐元轉換數字顯示出來。使用函數在vb.net中生成一個歐元轉換器

我相信這是通過告訴系統將輸入的任何數字乘以1.34509的轉換率完成的。

但我不認爲我會做正確的。

Protected Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click 

    End Sub 

    Function Convert(txtPound As Decimal) As Decimal 
     'declare variable to store answer 
     Dim Converter As Decimal 
     'calculate the Answer 
     Converter = Convert(txtPound * 1.34509) 
     'return the Answer 
     Return Converter 

    End Function 

End Class 
+0

從不讀值,調用函數或在點擊事件處理程序中設置的標籤除了,'Convert'函數看起來幾乎沒問題 - 你需要從文本框中取出一個「decimal」。 – Oded

+0

六行可壓縮爲一行:'Return txtPound * 1.34509'。順便說一句,這是一個非常糟糕的變量名稱:'txt'前綴意味着類型是'String',而不是'Decimal'。 –

+0

@KonradRudolph - 'txtPound'很可能是控件的名稱。 – Oded

回答

0

這將滿足您的要求,

事件:

'---This is the procedure which is going to handle your btnGo's Click Event 
Protected Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click 

'---Here we are calling the function Convert(Decimal) by passing the txtPound's text 
    msgbox(Convert(Decimal.Parse(txtPound.Text))) 

End Sub 

功能:

'---This function will receive the passed value as xValue and execute its code 
Private Function Convert(xValue as Decimal) as Decimal 

'---This line will do the conversion and simply send back the result to the caller. 
    return (xValue * 1.34509) 

End Function 

而且Furtherly如果你需要更澄清,然後直接this.

+0

謝謝,但msgbox是什麼意思? – user1944225

+0

@ user1944225 MsgBox是一個帶有OK按鈕的對話框。它只會顯示我們傳入的文本。例如:MsgBox(「Hello world」) –

+0

也可以將xValue更改爲不同的名稱,或者必須將其保留爲xValue – user1944225

0

你需要獲得進入量,並將其轉換爲十進制,然後才能計算:

Dim pounds As Decimal 
pounds = Decimal.Parse(txtPound.Text) 
Converter = Convert(pounds * 1.34509) 
+0

感謝您的回答,但您如何以函數格式生成此文件 – user1944225

+0

@ user1944225 - 我沒有關注。這些語句可以放入一個函數中,就像你已經有的一樣。 – Oded

+0

好吧,「返回」方面又如何工作,它做了什麼 – user1944225