Page 191 - Open Soource Technologies 304.indd
P. 191
Unit 8: Understanding Function and Procedure
btnConvert Text Convert to Celsius
lblTempC Text Temperature (Celsius)
txtTempC ReadOnly True
Private Sub btnConvert_Click(...) Handles btnConvert.Click
Dim fahrenheitTemp, celsiusTemp As Double
fahrenheitTemp = CDbl(txtTempF.Text)
celsiusTemp = FtoC(fahrenheitTemp)
txtTempC.Text = CStr(celsiusTemp)
‘Note: The above four lines can be replaced with the single line
‘txtTempC.Text = CStr(FtoC(CDbl(txtTempF.Text)))
End Sub
function FtoC(ByVal t As Double) As Double
‘Convert Fahrenheit temperature to Celsius
Return (5/9) * (t –32)
End function
[Run, type 212 into the text box, and then click the button.]
2: The following program uses the function FirstName.
LOVELY PROFESSIONAL UNIVERSITY 185