Help me find where is the mistake. This is a simple program converting
Fahrenheit into Celsius and Celsius into Fahrenheit.
Note that Fahrenheit to Celsius should be done in procedural way but
Celsius to Fahrenheit should be done by calling the function.
I did my coding but I get this error when I run it. What is wrong?
?Option Strict On disallows implicit conversions from 'Double' to 'Decimal'.?
This error refers to following line of code in Private function CalcFahr:
?CalcFahr = ((9 / 5) * decCalc) + 32?
Here is the code:
Public Class frmTempConverter
Inherits System.Windows.Forms.Form
.
.
.
.
Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles mnuFileExit.Click
'Closes and exits the program
Me.Close()
End Sub
Private Sub mnuFileFahrenheitToCelcius_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
mnuFileFahrenheitToCelcius.Click
'To convert a Fahrenheit temperature into Celsius: Tc = (5/9)*(Tf-32)
'Where Tc = Temperature in Celsius
'Where Tf = Temperature in Fahrenheit
Dim decFahrenheit As Decimal
Dim decCelsius As Decimal
If txtTemp.Text <> "" Then
If IsNumeric(txtTemp.Text) Then
decFahrenheit = CDec(txtTemp.Text)
decCelsius = (5D / 9D) * (decFahrenheit - 32D)
lblResult.Text = txtTemp.Text & " degrees Fahrenheit =
" & FormatNumber(decCelsius, 1) & " degrees celsius"
Else
MessageBox.Show("Temperature to convert must be
numeric", "Data entry error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
Else
MessageBox.Show("Must enter a number for temperature to
convert", "Temperature missing", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End Sub
Private Sub mnuFileCelciusToFahrenheit_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
mnuFileCelciusToFahrenheit.Click
'To convert a Celsius temperature into degrees Fahrenheit: Tf
= ((9/5)*Tc)+32
'Where Tc = Temperature in Celsius
'Where Tf = Temperature in Fahrenheit
Dim decFahrenheit As Decimal
Dim decCelsius As Decimal
If txtTemp.Text <> "" Then
If IsNumeric(txtTemp.Text) Then
decCelsius = CDec(txtTemp.Text)
lblResult.Text = txtTemp.Text & " degrees Celsius = "
& FormatNumber(CalcFahr(decCelsius)) 'Call function procedure
Else
MessageBox.Show("Temperature to convert must be
numeric", "Data entry error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
Else
MessageBox.Show("Must enter a number for temperature to
convert", "Temperature missing", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End Sub
Private Sub mnuEditClear_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles mnuEditClear.Click
'Clears temperature to convert and result
With txtTemp
.Clear()
.Focus()
End With
lblResult.Text = ""
End Sub
Private Function CalcFahr(ByVal decCalc As Decimal) As Decimal
'Calculate Farenheit from Celsius
CalcFahr = ((9 / 5) * decCalc) + 32
End Function
Private Sub frmTempConverter_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class |