Back
Convert Temperature.

Author: Guitarman 05/08/2021
Language: Visual Basic .NET


Description:

Functions to convert temperatures in VB.Net

Article:

#Region "Convert Temperature"
    Public Enum Temperature As Integer
        Rankine = 1
        Fahrenheit = 2
        Celsius = 3
        Kelvin = 4
        'Rømer = 5
        'Réaumur = 6
        'Newton = 7
    End Enum

    ''' <summary>
    ''' Converts Celsius, Fahrenheit, Kelvin and Rankine
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <param name="ConvertFrom"></param>
    ''' <param name="ConvertTo"></param>
    ''' <returns>Double</returns>
    ''' <remarks></remarks>
    Public Shared Function TemperatureConversion(ByVal Value As Single, ByVal ConvertFrom As Temperature, ByVal ConvertTo As Temperature) As Double
        Dim ReturnValue As Double = 0

        'Celsius to Fahrenheit Conversion
        If ConvertFrom = Temperature.Celsius And ConvertTo = Temperature.Fahrenheit Then

            ReturnValue = Value * 1.8 + 32

        End If

        'Celsius to Kelvin Conversion
        If ConvertFrom = Temperature.Celsius And ConvertTo = Temperature.Kelvin Then

            ReturnValue = Value + 273.15

        End If

        'Celsius to Rankine Conversion
        If ConvertFrom = Temperature.Celsius And ConvertTo = Temperature.Rankine Then

            ReturnValue = Value * 1.8 + 491.67

        End If

        'Fahrenheit to Celsius Conversion
        If ConvertFrom = Temperature.Fahrenheit And ConvertTo = Temperature.Celsius Then

            ReturnValue = (Value - 32) / 1.8

        End If

        'Fahrenheit to Kelvin Conversion
        If ConvertFrom = Temperature.Fahrenheit And ConvertTo = Temperature.Kelvin Then

            ReturnValue = (Value + 459.67) * 0.555

        End If

        'Fahrenheit to Rankine Conversion
        If ConvertFrom = Temperature.Fahrenheit And ConvertTo = Temperature.Rankine Then

            ReturnValue = Value + 459.67

        End If

        'Kelvin to Celsius Conversion
        If ConvertFrom = Temperature.Kelvin And ConvertTo = Temperature.Celsius Then

            ReturnValue = Value - 273.15

        End If

        'Kelvin to Fahrenheit Conversion
        If ConvertFrom = Temperature.Kelvin And ConvertTo = Temperature.Fahrenheit Then

            ReturnValue = Value * 1.8 - 459.67

        End If

        'Kelvin to Rankine Conversion
        If ConvertFrom = Temperature.Kelvin And ConvertTo = Temperature.Rankine Then

            ReturnValue = Value * 1.8

        End If


        'Rankine to Celsius Conversion
        If ConvertFrom = Temperature.Rankine And ConvertTo = Temperature.Celsius Then

            ReturnValue = Value * 0.555 - 273.15

        End If

        'Rankine to Fahrenheit Conversion
        If ConvertFrom = Temperature.Rankine And ConvertTo = Temperature.Fahrenheit Then

            ReturnValue = Value - 459.67

        End If

        'Rankine to Kelvin Conversion
        If ConvertFrom = Temperature.Rankine And ConvertTo = Temperature.Kelvin Then

            ReturnValue = Value * 0.555

        End If

        Return ReturnValue
    End Function
#End Region


Back
Comments
Add Comment
There are no comments yet.