Skip to content

VBMAN.ToolsArray - Array Tool Object ​

Overview ​

VBMAN.ToolsArray provides utility functions for array operations, including array element deletion, destructuring assignment, slicing, searching, etc.

Methods ​

Remove ​

Delete element at specified index from array

vb
Public Function Remove(ByRef Arr As Variant, ByVal Index As Integer) As Boolean

Parameters:

  • Arr - Array (passed by reference)
  • Index - Index to delete

Returns: True=success, False=failure

Description:

  • Moves elements after specified index forward
  • Automatically adjusts array size

Example:

vb
Dim arr(3) As String
arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"

' Delete element at index 1 (B)
VBMAN.ToolsArray.Remove arr, 1

' Result: A, C, D
Debug.Print arr(0)  ' A
Debug.Print arr(1)  ' C
Debug.Print arr(2)  ' D

DeArray ​

Destructure array to variables

vb
Public Sub DeArray(Arr As Variant, ParamArray OutVars())

Parameters:

  • Arr - Source array
  • OutVars - Output variable array

Description:

  • Assigns array elements to variables in order
  • Only assigns partial elements if insufficient variables

Example:

vb
Dim a As String, b As String, c As String

' Destructure array
VBMAN.ToolsArray.DeArray Split("Apple/Banana/Orange", "/"), a, b, c

Debug.Print a  ' Apple
Debug.Print b  ' Banana
Debug.Print c  ' Orange

' Only take first two
Dim x As String, y As String
VBMAN.ToolsArray.DeArray Array(1, 2, 3, 4), x, y
Debug.Print x  ' 1
Debug.Print y  ' 2

GetIndexByValue ​

Find index by value

vb
Public Function GetIndexByValue(Arr As Variant, Value As String) As Long

Parameters:

  • Arr - Array
  • Value - Value to find

Returns: Index value, returns -1 if not found

Example:

vb
Dim fruits(3) As String
fruits(0) = "apple"
fruits(1) = "banana"
fruits(2) = "orange"
fruits(3) = "grape"

Dim idx As Long
idx = VBMAN.ToolsArray.GetIndexByValue(fruits, "orange")
Debug.Print idx  ' 2

idx = VBMAN.ToolsArray.GetIndexByValue(fruits, "watermelon")
Debug.Print idx  ' -1

IsArrayEmpty ​

Check if array is empty

vb
Public Function IsArrayEmpty(Arr As Variant) As Boolean

Parameters:

  • Arr - Array

Returns: True=empty array, False=not empty

Example:

vb
Dim emptyArr() As String
Dim arr(1) As String
arr(0) = "test"

Debug.Print VBMAN.ToolsArray.IsArrayEmpty(emptyArr)  ' True
Debug.Print VBMAN.ToolsArray.IsArrayEmpty(arr)       ' False

IsControlArray ​

Check if control is a control array

vb
Public Function IsControlArray(Ctl As Object) As Boolean

Parameters:

  • Ctl - Control object

Returns: True=is control array, False=is not

Example:

vb
' Check if Command1 is a control array
If VBMAN.ToolsArray.IsControlArray(Command1) Then
    Debug.Print "Command1 is a control array"
End If

SliceByteArray ​

Byte array slicing

vb
Public Function SliceByteArray(ByRef Arr() As Byte, ByVal StartPos As Long, Optional ByVal EndPos As Long = -1) As Byte()

Parameters:

  • Arr - Byte array
  • StartPos - Start position
  • EndPos - End position (-1 means to end)

Returns: Sliced byte array

Example:

vb
Dim bytes(9) As Byte
' ... fill data

' Get bytes 2-5
Dim sliced() As Byte
sliced = VBMAN.ToolsArray.SliceByteArray(bytes, 2, 5)

Comprehensive Examples ​

Example 1: Dynamic Array Element Deletion ​

vb
Private Sub RemoveFromArray()
    Dim items As Variant
    items = Array("Apple", "Banana", "Orange", "Grape")
    
    ' Delete "Banana" (index 1)
    VBMAN.ToolsArray.Remove items, 1
    
    Dim i As Long
    For i = LBound(items) To UBound(items)
        Debug.Print items(i)
    Next i
    ' Output: Apple, Orange, Grape
End Sub

Example 2: Batch Variable Assignment ​

vb
Private Sub ParsePath()
    Dim path As String
    path = "C:\\Users\\Admin\\Documents\\file.txt"
    
    Dim drive As String, user As String, folder As String, file As String
    
    ' Destructure path
    VBMAN.ToolsArray.DeArray Split(path, "\\"), drive, user, folder, file
    
    Debug.Print "Drive: " & drive   ' C:
    Debug.Print "User: " & user      ' Users
    Debug.Print "Folder: " & folder  ' Admin
    Debug.Print "File: " & file      ' Documents
End Sub

Example 3: Find and Delete ​

vb
Private Sub RemoveByValue()
    Dim items() As String
    items = Split("Apple,Banana,Orange,Grape", ",")
    
    ' Find index
    Dim idx As Long
    idx = VBMAN.ToolsArray.GetIndexByValue(items, "Orange")
    
    If idx >= 0 Then
        ' Delete found element
        VBMAN.ToolsArray.Remove items, idx
        MsgBox "Orange deleted"
    Else
        MsgBox "Orange not found"
    End If
End Sub

Best Practices ​

  1. Index Check: Check index validity before deleting or accessing array
  2. Destructuring Assignment: DeArray is suitable for processing fixed-format data parsing
  3. Array Bounds: Note that VB6 arrays can have custom starting index
  4. Type Matching: GetIndexByValue performs string comparison, pay attention to type conversion

VB6 and LOGO copyright of Microsoft Corporation