# I need to write a program to calculate temperature

Page 1 of 1

## 4 Replies - 5492 Views - Last Post: 10 November 2009 - 11:41 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137924&amp;s=3177e82839552787f411815d2bf8bddf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bgile

Reputation: 1
• Posts: 35
• Joined: 29-September 09

# I need to write a program to calculate temperature

Posted 09 November 2009 - 06:49 PM

I am writing a program to calculate Fahrenheit to Celsius and Celsius to Fahrenheit.
I just can not get my math to work. thanks for any help.
Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm
Private Sub CalcFahren(ByRef ftemp As Integer)
Dim totalTemp As Decimal
totalTemp = CDec(CDbl(TempTextBox.Text) * 9 / 5 + 32)
End Sub
Private Sub CalcCelsisus(ByRef ctemp As Integer)
Dim cTotalTemp As Decimal
cTotalTemp = CDec((CDbl(TempTextBox.Text) - 32) * 5 / 9)

End Sub
Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub calcCelsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcCelsButton.Click
Dim temp As Decimal
Call CalcCelsisus(CInt(temp))

celsLabel.Text = temp.ToString("N2")

TempTextBox.Focus()
TempTextBox.SelectAll()

End Sub

Private Sub calcFehrenButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcFehrenButton.Click

Dim secTemp As Decimal

Call CalcFahren(CInt(secTemp))

fahrenLabel.Text = secTemp.ToString("N2")

TempTextBox.Focus()
TempTextBox.SelectAll()

End Sub
End Class

Is This A Good Question/Topic? 0

## Replies To: I need to write a program to calculate temperature

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 06:45 AM

Off to VB.NET.

### #3 mark.bottomley

Reputation: 176
• Posts: 990
• Joined: 22-April 09

## Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 07:24 AM

You are not getting the correct data into the Sub and you are not retiurning any result from the calculation.
' A function is a Sub that returns a result - it can be returned with either a Return statement, or the name of the Function can be used as the variable that will automatically be returned.
Private Function CalcFahren(ByRef ftemp As Decimal) As Decimal
CalcFahren= CDec(ftemp) * 9 / 5 + 32
' alternate - "Return CDec(ftemp) * 9 / 5 + 32"
End Function

' and call it with
Dim resultTemp As Decimal
resultTemp  = CalcFahren(CDec(TempTextBox.Text))
' now output it somewhere
...

### #4 dzone41

Reputation: 46
• Posts: 218
• Joined: 21-August 09

## Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 07:32 AM

Starting with calcCelsButton.Click:
"temp" is not assigned a value before it is used, so an error will occur.
Suggest making CalcCelsisus a function and setting the value of temp to that function like:
temp = CalcCelsisus()

And the function
''''Watch out for the spelling, I have corrected it here
Private Function CalcCelsius() As Decimal
Dim cTotalTemp As Decimal
cTotalTemp = CDec((CDbl(TempTextBox.Text) - 32) * 5 / 9)
Return cTotalTemp
End Function

Try that with the other button press and routine for calculating Fahrenheit.
Hope this helps

This post has been edited by dzone41: 10 November 2009 - 07:33 AM

Reputation: 46
• Posts: 157
• Joined: 26-May 09

## Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 11:41 AM

Your code is more complicated than it needs to be.
Take a look at the code below.

Public Class Form3

Private Function CelciusToFahrenheit(ByVal Tc As Decimal) As Decimal
Dim Tf As Decimal
Tf = (9 / 5) * Tc + 32
Return Tf
End Function

Private Function FahrenheitToCelcius(ByVal Tf As Decimal) As Decimal
Dim tc As Decimal
Tc = (5 / 9) * (Tf - 32)
Return TC
End Function

Private Sub txtFahrenheit_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFahrenheit.KeyUp
If txtCelsius.Text <> String.Empty Then
txtCelsius.Text = FahrenheitToCelcius(txtFahrenheit.Text)
Else
txtCelsius.Text = ""
End If
End Sub

Private Sub txtCelsius_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCelsius.KeyUp
If txtCelsius.Text <> String.Empty Then
txtFahrenheit.Text = CelciusToFahrenheit(txtCelsius.Text)
Else
txtFahrenheit.Text = ""
End If
End Sub
End Class