Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,202 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,456 people online right now. Registration is fast and FREE... Join Now!





Temperature Converter

This is a temperature converter function in VB.Net. Not only does it do Celsius & Fahrenheit but it also does Kelvins conversions as well

Submitted By: PsychoCoder
Actions:
Rating:
Views: 2,735

Language: VB.NET

Last Modified: July 26, 2007
Instructions: To use this function is simple, the 3 parameters (all required are:
InitialTemp: The starting temperature
ConvFrom (Item in TempUnits Enum): What are you converting it from?
ConvTo (Item in TempUnits Enum): What do you want it converted to?

Snippet


  1. 'Enumeration to hold the temp types
  2. Enum TempUnits
  3.     Kelvins = 0
  4.     Celsius = 1
  5.     Fahrenheit = 2
  6. End Enum
  7.  
  8. 'Now the conversion function
  9. Public Function TempConverter(ByVal InitialTemp As Double, ByVal ConvFrom As TempUnits, ByVal ConvTo As TempUnits) As Double
  10.     Dim Temp As Double
  11.     'First make sure they didnt enter same values for convert from & convert to
  12.     If ConvFrom = ConvTo Then
  13.         'Return initial value
  14.         Temp = InitialTemp
  15.     Else
  16.         Try
  17.             'Now decide what they want done
  18.             Select Case ConvFrom
  19.                 'Convert from Kelvins
  20.                 Case TempUnits.Kelvins
  21.                     'To Celsius
  22.                     If ConvTo = TempUnits.Celsius Then
  23.                         'Celsius = Kelvin - 273.15
  24.                         Temp = InitialTemp - 273.15
  25.                         'To Fahrenheit
  26.                     ElseIf ConvTo = TempUnits.Fahrenheit Then
  27.                         Temp = (InitialTemp - 273.15) * 1.8 + 32.0#
  28.                     End If
  29.                 'Convert from Celsius
  30.                 Case TempUnits.Celsius
  31.                     'To Kelvins
  32.                     If ConvTo = TempUnits.Kelvins Then
  33.                         'Kelvin = Celsius + 273.15
  34.                         Temp = InitialTemp + 273.15
  35.                         'To Fahrenheit
  36.                     ElseIf ConvTo = TempUnits.Fahrenheit Then
  37.                         'degree F = degree C x 1.8 + 32.
  38.                         Temp = InitialTemp * 1.8 + 32.0#
  39.                     End If
  40.                 'Convert from Fahrenheit
  41.                 Case TempUnits.Fahrenheit
  42.                     'To Celsius
  43.                     If ConvTo = TempUnits.Celsius Then
  44.                         'degree C = (degree F - 32.) / 1.8
  45.                         Temp = (InitialTemp - 32.0#) / 1.8
  46.                         'To Kelvins
  47.                     ElseIf ConvTo = TempUnits.Kelvins Then
  48.                         Temp = ((InitialTemp - 32.0#) / 1.8) + 273.15
  49.                     End If
  50.             End Select
  51.         Catch ex As Exception
  52.             MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  53.             Temp = 0
  54.         End Try
  55.     End If
  56.     Return Temp
  57. End Function

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month