Public Module Inline Public Enum Apply Before After End Enum Public Function Assign(Of T)(ByRef value As T, replacement As T) As T value = replacement : Return value End Function ' Byte ' SByte ' Char ' Short ' UShort ' Integer Public Function Incr(ByRef value As Integer, Optional apply As Apply = Apply.After) As Integer If apply = Apply.Before Then Return Threading.Interlocked.Increment(value) Else Return Math.Min(Threading.Interlocked.Increment(value), value - 1) End If End Function Public Function Decr(ByRef value As Integer, Optional apply As Apply = Apply.After) As Integer If apply = Apply.Before Then Return Threading.Interlocked.Decrement(value) Else Return Math.Max(Threading.Interlocked.Decrement(value), value + 1) End If End Function ' UInteger Public Function Incr(ByRef value As UInteger, Optional apply As Apply = Apply.After) As UInteger If apply = Apply.Before Then Return Threading.Interlocked.Increment(value) Else Return CUInt(Math.Min(Threading.Interlocked.Increment(value), value - 1)) End If End Function Public Function Decr(ByRef value As UInteger, Optional apply As Apply = Apply.After) As UInteger If apply = Apply.Before Then Return Threading.Interlocked.Decrement(value) Else Return CUInt(Math.Max(Threading.Interlocked.Decrement(value), value + 1)) End If End Function ' Long Public Function Incr(ByRef value As Long, Optional apply As Apply = Apply.After) As Long If apply = Apply.Before Then Return Threading.Interlocked.Increment(value) Else Return Math.Min(Threading.Interlocked.Increment(value), value - 1) End If End Function Public Function Decr(ByRef value As Long, Optional apply As Apply = Apply.After) As Long If apply = Apply.Before Then Return Threading.Interlocked.Decrement(value) Else Return Math.Max(Threading.Interlocked.Decrement(value), value + 1) End If End Function ' ULong Public Function Incr(ByRef value As ULong, Optional apply As Apply = Apply.After) As ULong If apply = Apply.Before Then Return Threading.Interlocked.Increment(value) Else Return CULng(Math.Min(Threading.Interlocked.Increment(value), value - 1)) End If End Function Public Function Decr(ByRef value As ULong, Optional apply As Apply = Apply.After) As ULong If apply = Apply.Before Then Return Threading.Interlocked.Decrement(value) Else Return CULng(Math.Max(Threading.Interlocked.Decrement(value), value + 1)) End If End Function ' Single ' Double End Module