
''' <summary>
''' Provides a set of utilities on value types.
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class ValueUtil

    Public Shared Function TryParse(Of TEnum As Structure)(ByVal s As String, ByRef output As TEnum) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Dim enumType = GetType(TEnum)
        If enumType Is Nothing Then _
            Return False
        Try
            output = CType(CType([Enum].Parse(enumType, s, False), Object), TEnum)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Boolean) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Boolean.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Long) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Long.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Integer) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Integer.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Int16) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Int16.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Decimal) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Decimal.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As Double) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = Double.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef output As DateTime) As Boolean
        If String.IsNullOrEmpty(s) Then _
            Return False
        Try
            output = DateTime.Parse(s)
            Return True
        Catch ex As Exception
            ' discard exception and report false
        End Try
        Return False
    End Function

End Class