Search code examples
excelvbadictionarytypeslate-binding

Declaring a custom data Type containing a late-bound Dictionary Object


Can a Custom Type be declared having as one of its elements a Dictionary Object that is late-bound? e.g. in a Standard Module or a Class Module:

Private Type InternalData
    sName as String
    cSheets as Collection    'cSheets as New Collection
    wBook as Workbook
    bFiles as CreateObject("Scripting.Dictionary")    'bFiles as New CreateObject("Scripting.Dictionary")
End Type

OR Is there an alternative way? Please note, i don't want to early bind the Dictionary.


Solution

  • You will handle your dictionary object similar to how you handle your workbook object. Declare your dictionary as type Object and then initialize it someplace else. For example, in a Form you could have:

    Private Type InternalData
        sName As String
        cSheets As Collection
        wBook As Workbook
        bFiles As Object
    End Type
    
    Public Sub TryIt()
       Dim id As InternalData
       Set id.bFiles = CreateObject("Scripting.Dictionary")
       id.bFiles.Add Key, Value
    End Sub
    

    Based on comments, here's how you could do it with a class. In a Class Module:

    Public sName As String
    Public cSheets As Collection
    Public wBook As WorkBook
    Public bFiles As Object
    
    Private Sub Class_Initialize()
       Set bFiles = CreateObject("Scripting.Dictionary")
    End Sub
    

    and then like before, perhaps in a Form:

    Public Sub TryIt()
       Dim id As InternalData
       id.bFiles.Add Key, Value
    End Sub