Tools - Array Tools
cToolsArray - Array Operation Tools
Overview
Provides practical methods for array operations, including removing elements, array extension, slicing, destructuring, etc.
Methods
Remove
Removes element at specified index from array, subsequent elements move forward automatically.
Public Function Remove(ByRef Arr As Variant, ByVal Index As Integer) As BooleanParameters:
| Parameter | Type | Description |
|---|---|---|
Arr | Variant | Array to operate on (ByRef passed) |
Index | Integer | Index of element to remove |
Return Value:
True- Removal successfulFalse- Removal failed (array empty or index out of bounds)
Example:
Dim Arr As Variant
Arr = Array("a", "b", "c", "d")
' Remove element at index 1 ("b")
VBMAN.ToolsArray.Remove Arr, 1
' Result: Arr = ["a", "c", "d"]
Debug.Print Join(Arr, ",") ' Output: a,c,dextend
Extends values from source array to target array.
Public Property Let extend(Vars As Variant, Value As Variant)Example:
Dim Target(0 To 2) As String
Dim Source As Variant
Source = Split("x/y/z", "/")
' Assign Source values to Target
VBMAN.ToolsArray.extend(Target) = Source
Debug.Print Target(0) ' Output: xDeArray
Destructures array to variables (assigns array elements to multiple variables).
Public Sub DeArray(Arr As Variant, ParamArray OutVars())Parameters:
| Parameter | Type | Description |
|---|---|---|
Arr | Variant | Source array |
OutVars | ParamArray | Output variable array |
Example:
Dim A As String, C As String
' Assign array element 0 to A, element 2 to C
VBMAN.ToolsArray.DeArray Split("a/b/c", "/"), A, , C
Debug.Print A ' Output: a
Debug.Print C ' Output: cIsArrayEmpty
Checks if array is empty.
Public Function IsArrayEmpty(Arr As Variant) As BooleanReturn Value:
True- Array is emptyFalse- Array is not empty
Example:
Dim EmptyArr() As Variant
Dim Arr As Variant
Arr = Array("a", "b")
Debug.Print VBMAN.ToolsArray.IsArrayEmpty(EmptyArr) ' Output: True
Debug.Print VBMAN.ToolsArray.IsArrayEmpty(Arr) ' Output: FalseGetIndexByValue
Finds index of value in array.
Public Function GetIndexByValue(Arr As Variant, Value As String) As LongParameters:
| Parameter | Type | Description |
|---|---|---|
Arr | Variant | Array to search |
Value | String | Value to find |
Return Value:
- Returns index if found (0-based)
- Returns -1 if not found
Example:
Dim Arr As Variant
Arr = Array("apple", "banana", "cherry")
Dim Index As Long
Index = VBMAN.ToolsArray.GetIndexByValue(Arr, "banana")
Debug.Print Index ' Output: 1IsControlArray
Checks if control is a control array.
Public Function IsControlArray(Ctl As Object) As BooleanExample:
' Check if Command1 is a control array
If VBMAN.ToolsArray.IsControlArray(Command1) Then
Debug.Print "Command1 is a control array"
End IfArray Slice Functions
SliceByteArray
Processes Byte array slicing.
Function SliceByteArray(ByRef Arr() As Byte, ByVal StartPos As Long, Optional ByVal EndPos As Long = -1) As Byte()Parameters:
| Parameter | Type | Description |
|---|---|---|
Arr | Byte() | Source byte array |
StartPos | Long | Start position (0-based) |
EndPos | Long | End position (0-based), -1 means to end |
Example:
Dim ByteArray() As Byte
Dim Sliced() As Byte
ByteArray = StrConv("Hello-World", vbFromUnicode)
Sliced = VBMAN.ToolsArray.SliceByteArray(ByteArray, 0, 4)
' Output slice result
Dim i As Long
For i = LBound(Sliced) To UBound(Sliced)
Debug.Print Chr(Sliced(i)); ' Output: Hello
Next iSliceString
Processes String type slicing.
Function SliceString(ByVal Arr As String, ByVal StartPos As Long, Optional ByVal EndPos As Long = -1) As StringExample:
Dim str As String
Dim sliced As String
str = "Hello World"
sliced = VBMAN.ToolsArray.SliceString(str, 0, 4)
Debug.Print sliced ' Output: HelloSliceLongArray
Processes Long type array slicing.
Function SliceLongArray(ByRef Arr() As Long, ByVal StartPos As Long, Optional ByVal EndPos As Long = -1) As Long()Example:
Dim LongArr() As Long
Dim Sliced() As Long
LongArr = Array(10, 20, 30, 40, 50, 60, 70)
Sliced = VBMAN.ToolsArray.SliceLongArray(LongArr, 2, 5)
' Output: 30, 40, 50, 60Byte Array Operation Functions
StringToByteArray
Converts string to byte array.
Public Function StringToByteArray(ByVal St As String) As Byte()Example:
Dim Bytes() As Byte
Bytes = VBMAN.ToolsArray.StringToByteArray("Hello")GetArrayLength
Gets array length.
Public Function GetArrayLength(Arr() As Variant) As LongExample:
Dim Arr As Variant
Arr = Array("a", "b", "c")
Debug.Print VBMAN.ToolsArray.GetArrayLength(Arr) ' Output: 3FindByteArray
Finds position of keyword byte array in byte array.
Public Function FindByteArray(ByRef Arr() As Byte, ByRef Keyword() As Byte) As LongReturn Value:
- Returns start position if found (0-based)
- Returns -1 if not found
Example:
Dim Data() As Byte
Dim Keyword() As Byte
Data = StrConv("Hello World, Hello VB", vbFromUnicode)
Keyword = StrConv("World", vbFromUnicode)
Dim Pos As Long
Pos = VBMAN.ToolsArray.FindByteArray(Data, Keyword)
Debug.Print Pos ' Output: 6SplitByteArray
Splits byte array into two arrays.
Public Function SplitByteArray(ByRef Arr() As Byte, ByVal Position As Long, Optional Offset As Long) As VariantReturn Value:
Returns Variant array containing two byte arrays:
- Index 0: First part
- Index 1: Second part (starting from Position + Offset)
Example:
Dim Data() As Byte
Dim Result As Variant
Data = StrConv("HelloWorldVB", vbFromUnicode)
Result = VBMAN.ToolsArray.SplitByteArray(Data, 5, 0)
' Result(0) = "Hello"
' Result(1) = "WorldVB"SplitByteArrayByKeyword
Splits byte array by keyword, supports multiple splits.
Function SplitByteArrayByKeyword(ByRef ByteArray() As Byte, ByVal Keyword As Variant, Optional ByVal SplitCount As Long) As VariantParameters:
| Parameter | Type | Description |
|---|---|---|
ByteArray | Byte() | Byte array to split |
Keyword | Variant | Keyword (supports string or byte array) |
SplitCount | Long | Split count, 0 means split all |
Return Value:
Returns Variant array, each element is a byte array.
Example:
Dim ByteArray() As Byte
Dim Keyword As String
Dim Result As Variant
Dim i As Long
ByteArray = StrConv("Hello World, This is a test. Hello World again!", vbFromUnicode)
Keyword = "World"
Result = VBMAN.ToolsArray.SplitByteArrayByKeyword(ByteArray, Keyword)
For i = LBound(Result) To UBound(Result)
Debug.Print "Part " & (i + 1) & ": " & StrConv(Result(i), vbUnicode)
Next icToolsList - Recordset Conversion Tools
Overview
Provides conversion functionality between ADODB.Recordset and Collection/Dictionary.
Methods
RsToCollection
Converts ADODB.Recordset to cCollection (dictionary array).
Public Function RsToCollection(Obj As Variant) As cCollectionParameters:
| Parameter | Type | Description |
|---|---|---|
Obj | Variant | ADODB.Recordset object |
Return Value:
Returns cCollection, where each element is a Scripting.Dictionary representing one record.
Example:
Dim Rs As New ADODB.Recordset
Dim Coll As cCollection
Dim Row As Scripting.Dictionary
' Execute query
Set Rs = Db.Execute("SELECT * FROM Users")
' Convert to collection
Set Coll = VBMAN.ToolsList.RsToCollection(Rs)
' Iterate results
Dim i As Long
For i = 1 To Coll.Count
Set Row = Coll(i)
Debug.Print Row("UserName") & " - " & Row("Email")
Next iDescription:
- Supports pagination (preserves original recordset's pagination state)
- Supports filters (preserves original recordset's Filter)
- Clones recordset for iteration, does not affect original recordset position