Advertisement
AlvinSeville7cf

Array Subroutines

Feb 27th, 2021 (edited)
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.81 KB | None | 0 0
  1. Public Low As Single
  2. Public High As Single
  3.  
  4. ' Filters source array with predicateName.
  5. Public Function Where(source() As Variant, predicateName As String) As Variant()
  6.     Const PredicateNameIsEmpty As Integer = 513
  7.    
  8.     If predicateName = "" Then
  9.         Call Err.Raise(PredicateNameIsEmpty, "predicateName", _
  10.             "empty predicate name can't be used")
  11.     End If
  12.    
  13.     Dim Result() As Variant
  14.    
  15.     Dim isFirst As Boolean
  16.     isFirst = True
  17.     For i = 0 To UBound(source)
  18.         Dim suites As Boolean
  19.         suites = Application.Run(predicateName, source(i))
  20.         If suites Then
  21.             If isFirst Then
  22.                 ReDim Preserve Result(0)
  23.                 isFirst = False
  24.             Else
  25.                 ReDim Preserve Result(UBound(Result) + 1)
  26.             End If
  27.             Result(UBound(Result)) = source(i)
  28.         End If
  29.     Next i
  30.     Where = Result
  31. End Function
  32.  
  33. ' Converts array to string.
  34. Public Function ArrayToString(source() As Variant, Optional delimiter As String = ", ", _
  35.     Optional startDelimiter As String = "[", Optional endDelimiter As String = "]")
  36.     Dim Result As String
  37.     Result = startDelimiter
  38.     On Error Resume Next
  39.     If UBound(source) >= LBound(source) Then
  40.         For Each Item In source
  41.             Result = Result & CStr(Item) & delimiter
  42.         Next
  43.     End If
  44.     ArrayToString = Left(Result, Len(Result) - Len(delimiter)) & endDelimiter
  45. End Function
  46.  
  47. Public Function IsGreaterZeroPredicate(value As Single) As Boolean
  48.     IsGreaterZeroPredicate = value > 0
  49. End Function
  50.  
  51. Public Function IsLessZeroPredicate(value As Single) As Boolean
  52.     IsLessZeroPredicate = value < 0
  53. End Function
  54.  
  55. Public Function IsInRangePredicate(value As Single) As Boolean
  56.     IsInRangePredicate = value >= Low And value <= High
  57. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement