How to clear the entire array?

前端 未结 8 881
不思量自难忘°
不思量自难忘° 2021-02-02 05:06

I have an array like this:

Dim aFirstArray() As Variant

How do I clear the entire array? What about a collection?

8条回答
  •  悲哀的现实
    2021-02-02 05:45

    i fell into a case where clearing the entire array failed with dim/redim :

    having 2 module-wide arrays, Private inside a userform,

    One array is dynamic and uses a class module, the other is fixed and has a special type.

    Option Explicit
    
    Private Type Perso_Type
       Nom As String
       PV As Single 'Long 'max 1
       Mana As Single 'Long
       Classe1 As String
       XP1 As Single
       Classe2 As String
       XP2 As Single
       Classe3 As String
       XP3 As Single
       Classe4 As String
       XP4 As Single
       Buff(1 To 10) As IPicture 'Disp
       BuffType(1 To 10) As String
       Dances(1 To 10) As IPicture 'Disp
       DancesType(1 To 10) As String
    End Type
    
    Private Data_Perso(1 To 9, 1 To 8) As Perso_Type
    
    Dim ImgArray() As New ClsImage 'ClsImage is a Class module
    

    And i have a sub declared as public to clear those arrays (and associated run-time created controls) from inside and outside the userform like this :

    Public Sub EraseControlsCreatedAtRunTime()
    Dim i As Long
    On Error Resume Next
    With Me.Controls 'removing all on run-time created controls of the Userform :
        For i = .Count - 1 To 0 Step -1 
            .Remove i
        Next i
    End With
    Err.Clear: On Error GoTo 0
    
    Erase ImgArray, Data_Perso
    'ReDim ImgArray() As ClsImage ' i tried this, no error but wouldn't work correctly
    'ReDim Data_Perso(1 To 9, 1 To 8) As Perso_Type 'without the erase not working, with erase this line is not needed.
    End Sub
    

    note : this last sub was first called from outside (other form and class module) with Call FormName.SubName but had to replace it with Application.Run FormName.SubName , less errors, don't ask why...

提交回复
热议问题