/ 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.
Expand |
Embed | Plain Text
NameSpace UOM Public Class Temperature ' Temperature values are always stored in Kelvin because it is the standard within the scientific community. Private m_temperatureKelvin as Double ' This function allows the user to change the value of the temperature after the class is instantiated. WriteOnly Property setTemperature() as String Set(ByVal temperature as String) ' Because the value can also be set in the constructor, the process is encapsulated in a single function. changeTemperature(temperature) End Set End Property ' Constructor Public Sub New(ByVal temperature as String) ' Because the value can also be set in the setTemperature() function, the process is encapsulated in a single function. changeTemperature(temperature) End Sub ' Determines the specified unit of measure and converts the value to Kelvin. Private Sub changeTemperature(ByVal temperature as String) Try Dim value as Double value = CDbl(Left(temperature, Instr(temperature, " "))) Dim unit as String unit = Right(temperature, Len(temperature) - Instrrev(temperature, " ")) Select Case unit.toUpper() Case "C" ' Celsius m_temperatureKelvin = value + 273.15 Case "D" ' Delisle m_temperatureKelvin = 373.15 - value * 2 / 3 Case "F" ' Fahrenheit m_temperatureKelvin = (value - 32) * 5 / 9 + 273.15 Case "K" ' Kelvin m_temperatureKelvin = value Case "N" ' Newton m_temperatureKelvin = value * 100 / 33 + 273.15 Case "RA" ' Rankine m_temperatureKelvin = value / 1.8 Case "RE" ' Reaumur m_temperatureKelvin = value * 5 / 4 + 273.15 Case "RO" ' Romer m_temperatureKelvin = (value - 7.5) * 40 / 21 + 273.15 End Select Catch e as Exception Throw End Try End Sub ' Converts the stored value from Kelvin to Celsius. ReadOnly Property getCelsius() as Double Get Return m_temperatureKelvin - 273.15 End Get End Property ' Converts the stored value from Kelvin to Delisle. ReadOnly Property getDelisle() as Double Get Return (373.15 - m_temperatureKelvin) * 3 / 2 End Get End Property ' Converts the stored value from Kelvin to Fahrenheit. ReadOnly Property getFahrenheit() as Double Get Return (m_temperatureKelvin - 273.15) * 9 / 5 + 32 End Get End Property ' Returns the stored value in Kelvin. ReadOnly Property getKelvin() as Double Get Return m_temperatureKelvin End Get End Property ' Converts the stored value from Kelvin to Newton. ReadOnly Property getNewton() as Double Get Return (m_temperatureKelvin - 273.15) * 33 / 100 End Get End Property ' Converts the stored value from Kelvin to Rankine. ReadOnly Property getRankine() as Double Get Return m_temperatureKelvin * 1.8 End Get End Property ' Converts the stored value from Kelvin to Reaumur. ReadOnly Property getReaumur() as Double Get Return (m_temperatureKelvin - 273.15) * 4 / 5 End Get End Property ' Converts the stored value from Kelvin to Romer. ReadOnly Property getRomer() as Double Get Return (m_temperatureKelvin - 273.15) * 21 / 40 + 7.5 End Get End Property End Class End NameSpace
You need to login to post a comment.
