Public Class cMeineFunktionen
    'Diese Klasse beinhaltet alle meine Funktionen, die in allen Projekten verwendet werden können

    Public Const LeerDatum As Date = #12/30/1899#   'wird als leerer Datumswert verwendet, da sonst Probleme bei Null/Date

    Public Shared Function SQLDatum(dat As Date) As String
        'Datum für SQLAbfrage umwandeln (31.01.1998 --> #1/31/1998#)
        SQLDatum = ""

        If Not IsDate(dat) Then Exit Function
        SQLDatum = "'" & DateAndTime.Day(dat) & "." & DateAndTime.Month(dat) & "." & DateAndTime.Year(dat) & " 00:00:00'"
        'SQLDatum = dat.ToString
    End Function

    Public Shared Function SQLDatumWithOutSemi(dat As Date) As String
        'Datum für SQLAbfrage umwandeln (31.01.1998 --> #1/31/1998#)
        SQLDatumWithOutSemi = ""

        If Not IsDate(dat) Then Exit Function
        SQLDatumWithOutSemi = "" & DateAndTime.Day(dat) & "." & DateAndTime.Month(dat) & "." & DateAndTime.Year(dat) & " 00:00:00"
        'SQLDatum = dat.ToString
    End Function

    Public Shared Function SQLDatumZeit(dat As Date) As String
        'Datum inklusive Zeit für SQLAbfrage umwandeln (31.01.1998 10:15 Uhr --> #1/31/1998 10:15:00#)
        SQLDatumZeit = ""


        If Not IsDate(dat) Then Exit Function
        SQLDatumZeit = "'" & DateAndTime.Day(dat) & "." & DateAndTime.Month(dat) & "." & DateAndTime.Year(dat) & " " &
                  DateAndTime.Hour(dat) & ":" & DateAndTime.Minute(dat) & ":" & DateAndTime.Second(dat) & "'"


    End Function


    Public Shared Function SQLDatumZeitWithOutSemi(dat As Date) As String
        'Datum inklusive Zeit für SQLAbfrage umwandeln (31.01.1998 10:15 Uhr --> #1/31/1998 10:15:00#)
        SQLDatumZeitWithOutSemi = ""


        If Not IsDate(dat) Then Exit Function
        SQLDatumZeitWithOutSemi = "" & DateAndTime.Day(dat) & "." & DateAndTime.Month(dat) & "." & DateAndTime.Year(dat) & " " &
                    DateAndTime.Hour(dat) & ":" & DateAndTime.Minute(dat) & ":" & DateAndTime.Second(dat) & ""


    End Function

    '  Public Shared Function GetProgrammIcon() As Drawing.Icon
    'Return My.Resources.uid
    'End Function

    Public Shared Function VarToInt(ByVal wert As Object) As Integer
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return 0
            Else
                Return CInt(wert)
            End If
        Catch generatedExceptionName As Exception
            Return 0
        End Try
    End Function

    Public Shared Function VarToLng(ByVal wert As Object) As Long
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return 0
            Else
                Return CLng(wert)
            End If
        Catch generatedExceptionName As Exception
            Return 0
        End Try
    End Function

    Public Shared Function VarToDbl(ByVal wert As Object) As Double
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return 0
            Else
                Return CDbl(wert)
            End If
        Catch generatedExceptionName As Exception
            Return 0
        End Try
    End Function

    Public Shared Function VarToBool(ByVal wert As Object) As Boolean
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return False
            Else
                Return CBool(wert)
            End If
        Catch generatedExceptionName As Exception
            Return False
        End Try
    End Function

    Public Shared Function VarToStr(ByVal wert As Object) As String
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return ""
            Else
                Return Trim(DirectCast(wert, String))
            End If
        Catch generatedExceptionName As Exception
            Return ""
        End Try
    End Function

    Public Shared Function VarToDate(ByVal wert As Object) As Nullable(Of DateTime)
        Try
            If wert Is Nothing OrElse wert Is DBNull.Value Then
                Return LeerDatum  'Nothing
            Else
                Return DirectCast(wert, DateTime)
            End If
        Catch generatedExceptionName As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function IstGleich(i As Integer, ParamArray list As Integer()) As Boolean
        'prüft, ob ein Wert in einer Liste enthalten ist - z.B. i = 5 or 7 or 11 or 29
        For x As Integer = 0 To list.Length - 1
            If list(x) = i Then
                Return True
            End If
            On Error Resume Next
        Next
        Return False
    End Function

    Public Shared Function Minuten_auf_Text(hMinuten As Long) As String
        Dim hStunden As Long
        Dim hMinus As Boolean
        Dim hMin As Long

        Minuten_auf_Text = ""
        hMin = hMinuten

        If hMin = 0 Then
            Minuten_auf_Text = "0:00"
            Exit Function
        End If

        If hMin < 0 Then
            hMinus = True
            hMin = hMin * -1
        End If

        hStunden = CLng(Fix(hMin / 60))
        hMin = hMin - (hStunden * 60)
        If hMinus Then Minuten_auf_Text = "-" 'Minus wird nur bei Stunden angezeigt
        Minuten_auf_Text = Minuten_auf_Text & hStunden & ":" & Format(hMin, "00")
    End Function

    Public Shared Function ZeitInMinuten(hDat As Date) As Long
        ZeitInMinuten = 0
        If Not IsDate(hDat) Then Exit Function
        ZeitInMinuten = Hour(hDat) * 60 + Minute(hDat)
    End Function


End Class
