Temperature class stores the value as Double and delegates most of the functionality to the Double implementation. : Double « Data Types « VB.Net






Temperature class stores the value as Double and delegates most of the functionality to the Double implementation.

 
Imports System.Globalization
Public Class Temperature
    Implements IComparable, IFormattable
    Public Overloads Function CompareTo(ByVal obj As Object) As Integer _
        Implements IComparable.CompareTo
        If TypeOf obj Is Temperature Then
            Dim temp As Temperature = CType(obj, Temperature)

            Return m_value.CompareTo(temp.m_value)
        End If
        Throw New ArgumentException("object is not a Temperature")
    End Function

    Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
        Implements IFormattable.ToString

        If Not (format Is Nothing) Then
            If format.Equals("F") Then
                Return [String].Format("{0}'F", Me.Value.ToString())
            End If
            If format.Equals("C") Then
                Return [String].Format("{0}'C", Me.Celsius.ToString())
            End If
        End If

        Return m_value.ToString(format, provider)
    End Function
    Public Shared Function Parse(ByVal s As String, ByVal styles As NumberStyles, ByVal provider As IFormatProvider) As Temperature
        Dim temp As New Temperature()

        If s.TrimEnd(Nothing).EndsWith("'F") Then
            temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
        Else
            If s.TrimEnd(Nothing).EndsWith("'C") Then
                temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
            Else
                temp.Value = Double.Parse(s, styles, provider)
            End If
        End If
        Return temp
    End Function
    Protected m_value As Double

    Public Property Value() As Double
        Get
            Return m_value
        End Get
        Set(ByVal Value As Double)
            m_value = Value
        End Set
    End Property

    Public Property Celsius() As Double
        Get
            Return (m_value - 32) / 1.8
        End Get
        Set(ByVal Value As Double)
            m_value = Value * 1.8 + 32
        End Set
    End Property
End Class
Class Sample
   Public Shared Sub Main()

   End Sub 'Main

End Class

   
  








Related examples in the same category

1.Double Structure Represents a double-precision floating-point number.
2.Assign result to both an integer and a doubleAssign result to both an integer and a double
3.Double: Set number, multiply numbers, and display resultsDouble: Set number, multiply numbers, and display results
4.Double: Set number, divide numbers, and display resultsDouble: Set number, divide numbers, and display results
5.Double number format: 0:n3Double number format: 0:n3
6.Double value calculationDouble value calculation
7.Append Double data type values to a StringBuilder object.
8.Generic and nongeneric versions of the CompareTo method for Double value
9.Use the Sign(Double) method to determine the sign of a Double value and display it to the console.
10.Use Double.Parse to parse double string value
11.Convert Double.MinValue to string
12.Use CInt to convert Double to Integer
13.Double Structure Represents a double-precision floating-point number.
14.Double variable
15.Parse string to double value
16.Double ToString("#####")
17.Double Structure Represents a double-precision floating-point number.
18.Double.Epsilon Field Represents the smallest positive Double value that is greater than zero. This field is constant.
19.Double.Equals Method
20.Compare Double values with difference
21.Double.Parse Method
22.Double.ToString Method
23.Double.ToString Method Converts the numeric value of this instance to its equivalent string representation.
24.Parse a string read from console to Double
25.Double.TryParse Method (String, Double) Converts string to double-precision floating-point number