30-04-2009, 03:50 PM
Here's a real quick class that will use a byte array. You can insert, insert at certain indexes and remove at certain indexes.
I kinda tested it, if you see any problems let me know.
This is a quick example of how to use it.
This is the output:
I kinda tested it, if you see any problems let me know.
Code:
' **************
' * By : Dugor *
' **************
Option Explicit
' Our copy memory API call
Private Declare Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private bArr() As Byte ' A byte array we are using
Private Size As Long ' The size of the array
Private Sub Class_Initialize()
Size = -1
End Sub
' Will insert the value at the end of the array
Public Sub Insert(ByRef Value As Byte)
Size = Size + 1
ReDim Preserve bArr(Size)
bArr(Size) = Value
End Sub
' Will insert the value at the index you specify
' It will then move the rest of the array down
Public Sub InsertAt(ByRef Index As Long, ByRef Value As Byte)
If Index < 0 Then Exit Sub
' If the index you're trying to insert at is above our size
' just insert it at the end
If Index > Size Then
Insert Value
Else
Size = Size + 1
ReDim Preserve bArr(Size)
CopyMemory bArr(Index + 1), bArr(Index), Size - Index
bArr(Index) = Value
End If
End Sub
' Will remove a part of the array
Public Sub Remove(ByRef Index As Long)
If Index < 0 Then Exit Sub
' If this is the last element we have to handle it different
If Index = Size Then
Size = Size - 1
ReDim Preserve bArr(Size)
Else
If Index > Size Then Exit Sub
CopyMemory bArr(Index), bArr(Index + 1), Count - Index
Size = Size - 1
ReDim Preserve bArr(Size)
End If
End Sub
Public Function Count() As Long
Count = (UBound(bArr) - LBound(bArr)) + 1
End Function
Public Sub Display()
Dim i As Long
Debug.Print "Amount of items in array: ", Count
For i = LBound(bArr) To UBound(bArr)
Debug.Print "Index:"; i, "Value:", bArr(i)
Next
End Sub
This is a quick example of how to use it.
Code:
Dim t As New clsArray
t.Insert 1
t.Insert 2
t.Insert 8
t.InsertAt 1, 5
t.Display
t.Remove 3
t.Display
t.InsertAt 4, 10
t.Display
This is the output:
Quote:Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 8
Amount of items in array: 3
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 10