Public Class ClassConverter

    ' ------------- STRING TO TYPE -------------

    Public Shared Function ToBooleanOrDefault(value As Object, Optional defaultValue As Boolean = False)
        Try
            Dim bool As Boolean

            If Boolean.TryParse(value, bool) = True Then
                Return bool
            Else
                If IsDBNull(value) OrElse value = "" Then
                    Return defaultValue
                Else
                    Return Convert.ToBoolean(value)
                End If
            End If
        Catch ex As Exception
            Return defaultValue
        End Try
    End Function

    Public Shared Function ToStringOrDefault(value As Object, Optional defaultValue As String = "")
        Try
            If IsDBNull(value) OrElse String.IsNullOrEmpty(value) Then
                Return defaultValue
            Else
                Return Convert.ToString(value)
            End If
        Catch ex As Exception
            Return defaultValue
        End Try
    End Function


    Public Shared Function ToIntOrDefault(value As Object, Optional defaultValue As Integer = 0)
        Try
            If IsDBNull(value) Then
                Return defaultValue
            Else
                Return Convert.ToInt16(value)
            End If
        Catch ex As Exception
            Return defaultValue
        End Try
    End Function

    ' ------------- TYPE TO TYPE -------------

    Public Shared Function BoolToInt(bool As Boolean)
        Return IIf(bool, 1, 0)
    End Function

    Public Shared Function IntToBoolOrDefault(int As Integer, Optional defaultValue As Boolean = False)
        If int = 0 Then
            Return False
        ElseIf int = 1 Then
            Return True
        Else
            Return defaultValue
        End If
    End Function

    ' ------------- STRING TO ENUM -------------

    Public Shared Function ToDateTimePickerOptionsOrDefault(value As String)
        Try
            If IsDBNull(value) OrElse String.IsNullOrEmpty(value) Then
                Return EnumDateTimePickerDefaultValueOptions.Empty
            Else
                Dim result As EnumDateTimePickerDefaultValueOptions
                Dim success = [Enum].TryParse(Of EnumDateTimePickerDefaultValueOptions)(value, result)

                If success Then
                    Return result
                Else
                    Return EnumDateTimePickerDefaultValueOptions.Empty
                End If
            End If
        Catch ex As Exception
            Return EnumDateTimePickerDefaultValueOptions.Empty
        End Try
    End Function

    ' ------------- TYPE TO STRING -------------

    Public Shared Function SQLValueToString(value As SQLValue) As String
        Dim cmd As String = value.Value
        If cmd.Contains("'") Then
            cmd = cmd.Replace("'", "''")
        End If
        Return cmd
    End Function
End Class
