Posted By

Scooter on 05/11/11


Tagged

conversion temperature


Versions (?)

Temperature Class


 / Published in: VB.NET
 

URL: http://reusablecode.blogspot.com/2011/05/temperature-conversion-revisited.html

This class is intended to reside within the app_code folder in your ASP.NET web site. It facilitates the storage and conversion of temperature values. See the linked blog post for usage example.

  1. NameSpace UOM
  2. Public Class Temperature
  3. ' Temperature values are always stored in Kelvin because it is the standard within the scientific community.
  4. Private m_temperatureKelvin as Double
  5.  
  6. ' This function allows the user to change the value of the temperature after the class is instantiated.
  7. WriteOnly Property setTemperature() as String
  8. Set(ByVal temperature as String)
  9. ' Because the value can also be set in the constructor, the process is encapsulated in a single function.
  10. changeTemperature(temperature)
  11. End Set
  12. End Property
  13.  
  14. ' Constructor
  15. Public Sub New(ByVal temperature as String)
  16. ' Because the value can also be set in the setTemperature() function, the process is encapsulated in a single function.
  17. changeTemperature(temperature)
  18. End Sub
  19.  
  20. ' Determines the specified unit of measure and converts the value to Kelvin.
  21. Private Sub changeTemperature(ByVal temperature as String)
  22. Try
  23. Dim value as Double
  24. value = CDbl(Left(temperature, Instr(temperature, " ")))
  25.  
  26. Dim unit as String
  27. unit = Right(temperature, Len(temperature) - Instrrev(temperature, " "))
  28.  
  29. Select Case unit.toUpper()
  30. Case "C"
  31. ' Celsius
  32. m_temperatureKelvin = value + 273.15
  33. Case "D"
  34. ' Delisle
  35. m_temperatureKelvin = 373.15 - value * 2 / 3
  36. Case "F"
  37. ' Fahrenheit
  38. m_temperatureKelvin = (value - 32) * 5 / 9 + 273.15
  39. Case "K"
  40. ' Kelvin
  41. m_temperatureKelvin = value
  42. Case "N"
  43. ' Newton
  44. m_temperatureKelvin = value * 100 / 33 + 273.15
  45. Case "RA"
  46. ' Rankine
  47. m_temperatureKelvin = value / 1.8
  48. Case "RE"
  49. ' Reaumur
  50. m_temperatureKelvin = value * 5 / 4 + 273.15
  51. Case "RO"
  52. ' Romer
  53. m_temperatureKelvin = (value - 7.5) * 40 / 21 + 273.15
  54. End Select
  55. Catch e as Exception
  56. Throw
  57. End Try
  58. End Sub
  59.  
  60. ' Converts the stored value from Kelvin to Celsius.
  61. ReadOnly Property getCelsius() as Double
  62. Get
  63. Return m_temperatureKelvin - 273.15
  64. End Get
  65. End Property
  66.  
  67. ' Converts the stored value from Kelvin to Delisle.
  68. ReadOnly Property getDelisle() as Double
  69. Get
  70. Return (373.15 - m_temperatureKelvin) * 3 / 2
  71. End Get
  72. End Property
  73.  
  74. ' Converts the stored value from Kelvin to Fahrenheit.
  75. ReadOnly Property getFahrenheit() as Double
  76. Get
  77. Return (m_temperatureKelvin - 273.15) * 9 / 5 + 32
  78. End Get
  79. End Property
  80.  
  81. ' Returns the stored value in Kelvin.
  82. ReadOnly Property getKelvin() as Double
  83. Get
  84. Return m_temperatureKelvin
  85. End Get
  86. End Property
  87.  
  88. ' Converts the stored value from Kelvin to Newton.
  89. ReadOnly Property getNewton() as Double
  90. Get
  91. Return (m_temperatureKelvin - 273.15) * 33 / 100
  92. End Get
  93. End Property
  94.  
  95. ' Converts the stored value from Kelvin to Rankine.
  96. ReadOnly Property getRankine() as Double
  97. Get
  98. Return m_temperatureKelvin * 1.8
  99. End Get
  100. End Property
  101.  
  102. ' Converts the stored value from Kelvin to Reaumur.
  103. ReadOnly Property getReaumur() as Double
  104. Get
  105. Return (m_temperatureKelvin - 273.15) * 4 / 5
  106. End Get
  107. End Property
  108.  
  109. ' Converts the stored value from Kelvin to Romer.
  110. ReadOnly Property getRomer() as Double
  111. Get
  112. Return (m_temperatureKelvin - 273.15) * 21 / 40 + 7.5
  113. End Get
  114. End Property
  115. End Class
  116. End NameSpace

Report this snippet  

You need to login to post a comment.