Google Answers Logo
View Question
 
Q: VB.NETOption Strict On disallows implicit conversions from 'Double' to 'Decimal' ( No Answer,   1 Comment )
Question  
Subject: VB.NETOption Strict On disallows implicit conversions from 'Double' to 'Decimal'
Category: Computers > Programming
Asked by: karlovac-ga
List Price: $5.00
Posted: 23 Mar 2004 03:18 PST
Expires: 22 Apr 2004 04:18 PDT
Question ID: 319498
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
Answer  
There is no answer at this time.

Comments  
Subject: Re: VB.NETOption Strict On disallows implicit conversions from 'Double' to 'Decimal'
From: rookie17-ga on 24 Mar 2004 05:25 PST
 
try 

CalcFahr = (CType(9 / 5, Decimal) * decCalc) + 32

instead of 

CalcFahr = ((9 / 5) * decCalc) + 32

What is happening here, is that when you use "Option Strict On" the
statement (9/5) (one integer divided by anoher) results in an Double,
however your variable CalcFahr is a Decimal. I think when you use
"Option Strict On" this conversion is not legal, you have to explictly
do it yourself.

/rookie17

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy