Skip to content

Switch between Early and Late binding

Initial Setup

Also add EarlyBinding =1 or 0 into your VBA project Property conditional Compilation image

Step 1 : Create Class with If Else Block

Option Explicit

#If EarlyBinding = 1 Then
    Private m_dictionary As Dictionary
#Else
    Private m_dictionary As Object
#End If

Private Sub Class_Initialize()
    #If EarlyBinding = 1 Then
        Set m_dictionary = New Dictionary
    #Else
        Set m_dictionary = CreateObject("Scripting.Dictionary")
    #End If
End Sub

#If EarlyBinding = 1 Then
Public Property Get myDictionary() As Dictionary
 Set myDictionary = m_dictionary
End Property
#Else
Public Property Get myDictionary() As Object
 Set myDictionary = m_dictionary
#End If

End Property

Step2 : Implement Class in to your Module

Public Sub Test()
    Dim dict As New clsDict
    dict.myDictionary.Add "Vivek", 100
    dict.myDictionary.Add "Deven", 80
    dict.myDictionary.Add "Druv", 60

    'Count
    Debug.Print "Total Items:" & dict.myDictionary.Count

    'Loop Using For
    Dim i As Long
    For i = 0 To dict.myDictionary.Count - 1
        Debug.Print dict.myDictionary.Keys()(i), dict.myDictionary.Items()(i)
    Next i
End Sub
References - How to use Early and Late Binding the right way! - YouTube