Imports System.Runtime.CompilerServices Imports System.Web Public Module ModuleExtensions Const UnixEraStartTicks As Long = 621355968000000000 ' ====================================================== ' === DATETIME ' ====================================================== Public Function GetUnixTimestamp(pDate As Date) As Long Dim UnixEraTicks = pDate.Ticks - UnixEraStartTicks Return UnixEraTicks \ 10000 End Function Public Function DateFromUnix(pTimestamp As Long) As Date Return New Date(UnixEraStartTicks + pTimestamp * 10000) End Function ' ====================================================== ' === LIST ' ====================================================== Public Function JoinToString(pList As IEnumerable(Of String), pSeparator As Char) Return String.Join(pSeparator, pList) End Function ' ====================================================== ' === STRING ' ====================================================== ''' ''' Truncates a string to the specified length if it exceeds that length. ''' ''' The string ''' The maximum string length ''' The truncated string Public Function Truncate(pString As String, pLength As Integer) As String If String.IsNullOrEmpty(pString) Then Return pString Return pString.Substring(0, Math.Min(pLength, pString.Length)) End Function ''' ''' Replaces single quotes in text for SQL Commands. ''' ''' The string ''' The escaped string. Public Function EscapeForSQL(pString As String) As String Return ObjectEx.NotNull(pString, String.Empty).Replace("'", "''") End Function ''' ''' Converts a string to boolean. Accepts true and 1 as truthy values ''' ''' The input string ''' True if input is true or 1, otherwise false. Public Function ToBoolean(pString As String) As Boolean If String.IsNullOrEmpty(pString) Then Return False Return (pString.Trim().ToLower() = "true") OrElse (pString.Trim() = "1") End Function ''' ''' Checks if a string is null or empty ''' ''' The input string ''' True string is null or empty, otherwise false. Public Function IsNullOrEmpty(pString As String) As Boolean Return String.IsNullOrEmpty(pString) End Function ''' ''' Checks if a string is NOT null or empty ''' ''' The input string ''' True string is null or empty, otherwise false. Public Function IsNotNullOrEmpty(pString As String) As Boolean If String.IsNullOrEmpty(pString) Then Return False Else Return True End If End Function ' ====================================================== ' === DICTIONARY ' ====================================================== Public Function ToURLQueryString(pDictionary As IDictionary(Of String, String)) As String Dim oQueryString = HttpUtility.ParseQueryString(String.Empty) For Each oItem As KeyValuePair(Of String, String) In pDictionary oQueryString.Add(oItem.Key, oItem.Value) Next Return oQueryString.ToString() End Function ' ====================================================== ' === DATATABLE ' ====================================================== Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T Try If TableContainsColumn(pRow.Table, pFieldName) = False Then Return pDefaultValue Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue) Catch ex As Exception Return Nothing End Try End Function Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T Try If TableContainsColumn(pRow.Table, pFieldIndex) = False Then Return pDefaultValue Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue) Catch ex As Exception Return Nothing End Try End Function Public Function FieldOrDefault(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T Return ItemEx(pRow, pFieldName, pDefaultValue) End Function Public Function FieldOrDefault(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T Return ItemEx(pRow, pFieldIndex, pDefaultValue) End Function Public Function First(pTable As DataTable) As DataRow Try If pTable Is Nothing OrElse pTable.Rows.Count = 0 Then Return Nothing End If Return pTable.Rows.Item(0) Catch ex As Exception Return Nothing End Try End Function Private Function TableContainsColumn(pTable As DataTable, pColumnName As String) As Boolean Try If pTable Is Nothing Then Return False If String.IsNullOrEmpty(pColumnName) Then Return False Return pTable.Columns.Contains(pColumnName) Catch ex As Exception Return False End Try End Function Private Function TableContainsColumn(pTable As DataTable, pColumnIndex As Integer) As Boolean Try If pTable Is Nothing Then Return False If String.IsNullOrEmpty(pColumnIndex) Then Return False Return pTable.Columns.Count > pColumnIndex Catch ex As Exception Return False End Try End Function End Module