title: How to iterate dictionary?
body: <p>I am trying to iterate through dictionary but encounter the error "Object required". The related code is given below.</p>
  
  <p>First, I create a dictionary.</p>
  
  <pre><code>Dim customers
  Set customers = CreateObject("Scripting.Dictionary")
  </code></pre>
  
  <p>I also define class of "cCustomer", then use the dictionary like this.</p>
  
  <pre><code>Set customer = New cCustomer
  customer.Init location, first, last
  customers.Add location, customer
  </code></pre>
  
  <p>Then I use "For Each" to iterate.</p>
  
  <pre><code>Dim cc
  For Each cc in customers.items
    ...
  Next
  </code></pre>
  
  <p>This is OK. But I really want to declare "cc" with type.</p>
  
  <pre><code>Dim cc As cCustomer
  </code></pre>
  
  <p>But if I do this, VB runtime complains "Object required" at the line of "For Each". I think it is somehow related to the missing of type declaration when creating the dictionary? I am still new to VB. Thanks for the help.</p>
  
  <p>MORE: why this is not duplicate...</p>
  
  <p>I have tried the solution suggested by the link, namely, (a) using Dictionary instead of "Scripting.Dictionary", and (b) "Dim cc As Variant". It works as before but if I feed "cc" into a function whose argument has specific type it still fails.</p>
  
  <pre><code>Public Function foo(customer As cCustomer) As String
    ...
  End Function
  
  Dim cc As Variant
  For Each cc in customers.items
    foo(cc)
  Next
  </code></pre>
  
  <p>The error is "ByRef argument type mismatch".</p>
  
  <p>That's the reason I really need to declare "cc" as "cCustomer", but it has error of "Object required".</p>
  
  <pre><code>Dim cc As cCustomer
  For Each cc In customers.items
    ...
  </code></pre>
  
  <p>Per comments</p>
  
  <ul>
  <li>tried "Dim cc As Object", doesn't work ("Object required").</li>
  <li>tried "remove Dim cc", doesn't work either ("ByRef argument type mismatch").</li>
  </ul>
  
  <p>I could do "ByVal" in the function definition or use another variable, but that would involve extra copy. Something like type casting might help...</p>