Add a lot of functions to Base
This commit is contained in:
parent
00cff028c9
commit
b1114545a7
@ -77,7 +77,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BaseClass.vb" />
|
||||
<Compile Include="BaseUtils.vb" />
|
||||
<Compile Include="DatabaseEx.vb" />
|
||||
<Compile Include="ECM\ECM.vb" />
|
||||
<Compile Include="ModuleExtensions.vb" />
|
||||
<Compile Include="FileEx.vb" />
|
||||
<Compile Include="ObjectEx.vb" />
|
||||
<Compile Include="GraphicsEx.vb" />
|
||||
<Compile Include="IDB\Attributes.vb" />
|
||||
<Compile Include="IDB\Database.vb" />
|
||||
@ -101,6 +106,7 @@
|
||||
</Compile>
|
||||
<Compile Include="PerformanceEx.vb" />
|
||||
<Compile Include="ScreenEx.vb" />
|
||||
<Compile Include="StringEx.vb" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="My Project\Resources.resx">
|
||||
|
||||
7
Base/BaseUtils.vb
Normal file
7
Base/BaseUtils.vb
Normal file
@ -0,0 +1,7 @@
|
||||
Friend Class BaseUtils
|
||||
Friend Shared Function FormatHash(pChecksum)
|
||||
Return BitConverter.
|
||||
ToString(pChecksum).
|
||||
Replace("-", String.Empty)
|
||||
End Function
|
||||
End Class
|
||||
16
Base/DatabaseEx.vb
Normal file
16
Base/DatabaseEx.vb
Normal file
@ -0,0 +1,16 @@
|
||||
Public Class DatabaseEx
|
||||
''' <summary>
|
||||
''' Checks a Row value for three different `null` values,
|
||||
''' Nothing, Empty String, DBNull
|
||||
'''
|
||||
''' Returns the original value if the value is not null, or `defaultValue`
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The type of the value</typeparam>
|
||||
''' <param name="Row">The DataRow that contains the value</param>
|
||||
''' <param name="Column">The column name</param>
|
||||
''' <param name="DefaultValue">The default value</param>
|
||||
''' <returns>The original value or the default value</returns>
|
||||
Public Shared Function NotNull(Of T)(ByVal Row As DataRow, Column As String, DefaultValue As T) As T
|
||||
Return ObjectEx.NotNull(Row.Item(Column), DefaultValue)
|
||||
End Function
|
||||
End Class
|
||||
26
Base/FileEx.vb
Normal file
26
Base/FileEx.vb
Normal file
@ -0,0 +1,26 @@
|
||||
Imports System.IO
|
||||
Imports System.Security.Cryptography
|
||||
|
||||
Public Class FileEx
|
||||
''' <summary>
|
||||
''' Reads the file at `FilePath` and computes a SHA256 Hash from its contents
|
||||
''' </summary>
|
||||
''' <param name="pFilePath"></param>
|
||||
''' <returns></returns>
|
||||
Public Function GetChecksumFromFileContents(pFilePath As String) As String
|
||||
Try
|
||||
Using oFileStream = IO.File.OpenRead(pFilePath)
|
||||
Using oStream As New BufferedStream(oFileStream, 1200000)
|
||||
Dim oChecksum() As Byte = SHA256.Create.ComputeHash(oStream)
|
||||
Return BaseUtils.FormatHash(oChecksum)
|
||||
End Using
|
||||
End Using
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function GetHashFromFileContents(pFilePath As String) As String
|
||||
Return GetChecksumFromFileContents(pFilePath)
|
||||
End Function
|
||||
End Class
|
||||
101
Base/ModuleExtensions.vb
Normal file
101
Base/ModuleExtensions.vb
Normal file
@ -0,0 +1,101 @@
|
||||
Imports System.Runtime.CompilerServices
|
||||
|
||||
Public Module ModuleExtensions
|
||||
Const UnixEraStartTicks As Long = 621355968000000000
|
||||
|
||||
' ======================================================
|
||||
' === DATETIME
|
||||
' ======================================================
|
||||
|
||||
<Extension()>
|
||||
Public Function GetUnixTimestamp(pDate As Date) As Long
|
||||
Dim UnixEraTicks = pDate.Ticks - UnixEraStartTicks
|
||||
Return UnixEraTicks \ 10000
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function DateFromUnix(pTimestamp As Long) As Date
|
||||
Return New Date(UnixEraStartTicks + pTimestamp * 10000)
|
||||
End Function
|
||||
|
||||
' ======================================================
|
||||
' === STRING
|
||||
' ======================================================
|
||||
|
||||
''' <summary>
|
||||
''' Truncates a string to the specified length if it exceeds that length.
|
||||
''' </summary>
|
||||
''' <param name="pString">The string</param>
|
||||
''' <param name="pLength">The maximum string length</param>
|
||||
''' <returns>The truncated string</returns>
|
||||
<Extension()>
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Replaces single quotes in text for SQL Commands.
|
||||
''' </summary>
|
||||
''' <param name="pString">The string</param>
|
||||
''' <returns>The escaped string.</returns>
|
||||
<Extension()>
|
||||
Public Function EscapeForSQL(pString As String) As String
|
||||
Return ObjectEx.NotNull(pString, String.Empty).Replace("'", "''")
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts a string to boolean. Accepts true and 1 as truthy values
|
||||
''' </summary>
|
||||
''' <param name="pString">The input string</param>
|
||||
''' <returns>True if input is true or 1, otherwise false.</returns>
|
||||
<Extension()>
|
||||
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
|
||||
|
||||
' ======================================================
|
||||
' === DATATABLE
|
||||
' ======================================================
|
||||
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
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
|
||||
|
||||
<Extension()>
|
||||
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
|
||||
|
||||
<Extension()>
|
||||
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
|
||||
End Module
|
||||
46
Base/ObjectEx.vb
Normal file
46
Base/ObjectEx.vb
Normal file
@ -0,0 +1,46 @@
|
||||
Public Class ObjectEx
|
||||
''' <summary>
|
||||
''' Checks a value for three different `null` values,
|
||||
''' Nothing, Empty String, DBNull
|
||||
'''
|
||||
''' Returns the original value if the value is not null, or `defaultValue`
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The type of the value</typeparam>
|
||||
''' <param name="value">The value</param>
|
||||
''' <param name="defaultValue">The default Value</param>
|
||||
''' <returns>The original value or the default value</returns>
|
||||
Public Shared Function NotNull(Of T)(ByVal value As T, ByVal defaultValue As T) As T
|
||||
If IsNothing(value) OrElse String.IsNullOrEmpty(value.ToString) OrElse IsDBNull(value) Then
|
||||
Return defaultValue
|
||||
Else
|
||||
Return value
|
||||
End If
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts a String value to the given Enum
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The Enum Type</typeparam>
|
||||
''' <param name="value">The string value to convert</param>
|
||||
Public Shared Function ToEnum(Of T)(value As String) As T
|
||||
Return [Enum].Parse(GetType(T), value)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts an Integer value to the given Enum
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The Enum Type</typeparam>
|
||||
''' <param name="value">The integer value to convert</param>
|
||||
Public Shared Function ToEnum(Of T)(value As Integer) As T
|
||||
Return [Enum].ToObject(GetType(T), value)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts a Long value to the given Enum
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The Enum Type</typeparam>
|
||||
''' <param name="value">The long value to convert</param>
|
||||
Public Shared Function ToEnum(Of T)(value As Long) As T
|
||||
Return [Enum].ToObject(GetType(T), value)
|
||||
End Function
|
||||
End Class
|
||||
@ -45,4 +45,45 @@ Public Class ScreenEx
|
||||
|
||||
RestoreFormState(pForm, oFormState)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Checks if a point is Visible on any screen
|
||||
''' </summary>
|
||||
Public Shared Function IsVisibleOnAnyScreen(Location As Point) As Boolean
|
||||
Try
|
||||
Dim oRect As New Rectangle(Location, New Size(0, 0))
|
||||
|
||||
For Each oScreen In Screen.AllScreens
|
||||
If oScreen.WorkingArea.IntersectsWith(oRect) Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
|
||||
Return False
|
||||
Catch ex As Exception
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Checks if Size is not negative
|
||||
''' </summary>
|
||||
Public Shared Function SizeIsVisible(Size As Size) As Boolean
|
||||
If Size.Width >= 0 And Size.Height >= 0 Then
|
||||
Return True
|
||||
End If
|
||||
|
||||
Return False
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Checks if Location is not negative
|
||||
''' </summary>
|
||||
Public Shared Function LocationIsVisible(Location As Point) As Boolean
|
||||
If Location.X >= 0 And Location.Y >= 0 Then
|
||||
Return True
|
||||
End If
|
||||
|
||||
Return False
|
||||
End Function
|
||||
End Class
|
||||
|
||||
116
Base/StringEx.vb
Normal file
116
Base/StringEx.vb
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user