2

I'm using VBA in an application called ProcessBook and I'm looking to store some static information in a dictionary.

I'm trying to initialize this dictionary cleanly and without needing to call a separate procedure, but none of the methods I try seem to be expected.

Here's what I've tried so far:

Dim myDict As New Dictionary(Of String, String) (("key1", "item1"), ("key2", "item2"))
Dim myDict As Variant
Set myDict = New Dictionary( ("key1", "item1"), ("key2", "item2") )

And basically a bunch of guessing involving forms like that.

So far the best I've been able to do is something like:

With myDict
.add "key1", "item1"
.add "key2", "item2"
End With

But this barks at me when it's not inside of a routine, which I'd like to avoid.

I believe the (Of type, type) From { } syntax is beyond VBA 6.5

Does anyone know of some clean ways to initialize a scripting.dictionary in VBA 6.5?

3
  • 1
    the about box may report "VB 6.5" but it is actually VBScript and quite different from VB.NET Commented Jan 9, 2014 at 16:54
  • That's exactly what I did, thanks for the correction. Commented Jan 9, 2014 at 17:55
  • 1
    VBScript does not allow Dim var As Type syntax, this is Office VBA code (which has been referred to as VB 6.5 by Microsoft in the past) Commented Jan 9, 2014 at 19:11

2 Answers 2

1

By 6.5 presumably you mean VBA? If so there is no way to manipulate/initialise an object instance outside of a routine.

The only way is to encapsulate it within a class (cDict) and use the constuctor:

Private mDict As Scripting.Dictionary

Private Sub Class_Initialize()
    Set mDict = New Scripting.Dictionary
    With mDict
        .Add "key1", "item1"
        .Add "key2", "item2"
    End With
End Sub

Public Property Get Dict() As Scripting.Dictionary
    Set Dict = mDict
End Property

Then in the client module;

Private Dict As New cDict

Sub foo()
    MsgBox Dict.Dict.Item("key1")
    Dict.Dict.Add "key3", "item3"
    MsgBox Dict.Dict.Item("key3")
End Sub

(Of type relates to VB.Net Generics)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, that's unfortunate. I was hoping to be able to do something similar to Collection initialization which looked like: Dim myCol As Variant myCol = Array( "item1", "item2", "item3" )
1

VBA is really a PROCEDURAL language more then an Object Oriented Language so the answer is to use a procedure. How to do it...

Option Explicit
Option Compare Text

Sub test()
    Dim dic As New Scripting.Dictionary
    Add dic, Array("Darren", 123, "Alex", 321)
    MsgBox dic.Count
End Sub

Public Sub Add(dic As Dictionary, values As Variant)
    If Not IsArray(values) Then
        Err.Raise -1, "Add", "Values must be varient Array"
    ElseIf UBound(values) Mod 2 = 0 Then
        Err.Raise -1, "Add", "Even number of values required"
    End If
    Dim iQ As Long
    For iQ = 0 To UBound(values) Step 2
        dic.Add values(iQ), values(iQ + 1)
    Next
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.