Senipah / VBA-Better-Array

An array class for VBA providing features found in more modern languages
MIT License
115 stars 20 forks source link

[BUG] Empty items after Concat #7

Closed thenhustey closed 3 years ago

thenhustey commented 3 years ago

Describe the bug Using a 1D BetterArras for storing Filepaths from folder with recursion. Each (sub)folder returns its own BetterArray, which is "merged" with BetterArray from parent folder using Concat method.

Result 1D BetterArray contains a lot of empty Items.

To Reproduce Consider function:

Public Function GetFilesArrRecusively(ByVal Folder As Folder) As BetterArray
    Dim File As File
    Dim SubFolder As Folder
    Dim TempArray As BetterArray
    Set TempArray = New BetterArray

    ' Loops trought all files in given folder
    For Each File In Folder.Files
        ' Test for hidden and system files
        Dim IsHidden As Boolean
        Dim IsSystem As Boolean
        Dim Attributes As Long

        Attributes = File.Attributes
        IsHidden = Attributes And 2
        IsSystem = Attributes And 3

        If Not (IsHidden Or IsSystem) Then
            Call TempArray.Push(File.Path)
        End If
    Next File
    ' Now all files in the given folder are in the arr

    ' Go trought all subfolders of the given folder
    For Each SubFolder In Folder.SubFolders
        Dim SubFolderArr As BetterArray
        Set SubFolderArr = New BetterArray
        Set SubFolderArr = GetFilesArrRecusively(SubFolder)

        Dim TempArrClone As BetterArray
        Set TempArrClone = New BetterArray
        Set TempArrClone = TempArray.Clone ' Save state of TempArray before Concat

        ' Appends filepaths from subfolder
        If SubFolderArr.Length > 0 Then
            Call TempArray.Concat(SubFolderArr.Items)
            If TempArray.Includes(Empty) Then
                Debug.Print "EMPTY" '<------ Place breakpoint HERE
            End If
        End If
    Next SubFolder
    Set GetFilesArrRecusively = TempArray
End Function

Expected behavior 1D BetterArray filled with Filepaths (As String). For each file in given folder (except hidden and system files. Screenshots This are BetterArrays going to Concat - Call TempArray.Concat(SubFolderArr.Items) (remember: TempArr = TempArray.Clone) image

This is how TempArray looks like after Concat (see Items(40..63)) image

Operating System:

Host Application:

Additional context I debugged it step by step from my Concat call (line 36 in my code snippet). And when I stepped on your line 1402 (BetterArray.cls v1.7.3), found that Me.Items has 40 Items BUT This.Items has 64 items - all extra items are Empty. image image

Senipah commented 3 years ago

Hi @thenhustey,

Thanks so much for the bug report. After a fair amount of testing I found the bug - a simple silly error on my part. Concat() was calling this.Items instead of InternalItems.

This is fixed in the following commit: https://github.com/Senipah/VBA-Better-Array/commit/50e119abef7f92580ba7e746c42f3d7526822099

The change is released in 1.7.4

Please let me know if you still have issues and thanks again. 👍