PDA

View Full Version : Solved: Setting up a global dictionary



dfarmernv
09-17-2007, 11:48 AM
Hi everyone, first post here.

I could use a bit of help with the dictionary object. I am trying to write two functions, one that essentially places and order and one that receives an order (this is an inventory simulation macro). My idea is to just have a dictionary called orders and placing an order will just do
orders.Add [arrival-time], [quantity]
and on the receiving end it will (basically) look at the same dictionary and say, "Is the current time, 't', equal to the arrival time, 'at', if so return quantity."

The problem is I am getting all sorts of errors. At the moment I am just getting an error 1004 "Application-defined or object defined error" which I believe is coming from the Microsoft Scripting Engine. In Declarations I have
Dim orders As New Dictionary and then in the main loop of the simulation I have Set orders = New Dictionary (I'm not really sure where to put that statement, if I put it in the place or get orders functions it will get called on every iteration...). Anyway I hope my question is intelligible, if you need more code or explanation about what I'm attempting ask away. Thank you for any clues you can provide on setting up a global dictionary.

Bob Phillips
09-17-2007, 12:54 PM
Use


Dim orders As Object

Set orders = CreateObject("Scripting.Dictionary")

dfarmernv
09-17-2007, 01:36 PM
Thanks. That looks like it worked. I've still got other stuff to iron out but I think I've got it now!

I was wondering if you had time if you could explain the difference between the way I was invoking it and what you suggested (i.e., how did you come up with that? :think:)

Thanks again!

Bob Phillips
09-17-2007, 03:17 PM
Well the main thing was that you were NEWing the variable when you declared it, and then did it again by setting it. I NEVER New in the declaration, I do it in the setting.

Also, if you use the object type (Dictionary), you have to set a reference to the type library. This is called early-binding. You may or may not have done that, I had no way of knowing, so I used a generic object type (Object), and aligned it's type library when setting it (CreateObject), this is called late-binding.

dfarmernv
09-17-2007, 03:35 PM
Thanks so much! I hope my request didn't come across as rude, I appreciate you sharing your insight.

Bob Phillips
09-17-2007, 04:43 PM
Not in the least.