Programmically instantiate or change a Dictionary Object Name

DeadGoat

New Member
Joined
May 15, 2014
Messages
4
Hello,
I have a function designed to update a Dictionary object. All works well, but now I want to make the function more generic by having it instantiate a new dictionary object by passing a name. The motive is to use the function across different dictionaries. The problem is that the vba compiler won't compile the dictionary Name as it complains of a type mismatch.

In brief the function and code looks something like this: pass the name of the dictionary object I want to access, pass the operation I want to do, and an optional value.


Public Function GenericDictionary(dictName As String, dictMethod As String, optional dictValue as String ) As DictRtn


Select Case dictMethod
Case "instantiateDict"
Static dictName as Scripting.Dictionary
Set collectionName = New Scripting.Dictionary

……
Update DictRtn with something here….
….

End Select
return something here...

End Function



The complier won't accept the DictName in the Static statement. I believe I can understand why the type error is generated, but I need to insert the actual name that I pass with dictName and then have it compile to instantiate. I've attempted to do a DoCmd.rename, but the complier complains of undefined variables (ie DoCmd.rename dictName, Scripting.Dictionary, dictDefaultName) where dictDefaultName is something that I can predefine as a Scripting.Dictionary object.


Any suggestions or hints on how to get the compiler to type cast a string to a Scripting.Dictionary object? Suggestions on how to properly do the DoCmd Statement?

Thanks for any suggestions or help.

David
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Why can't you pass the actual Dictionary object to the procedure, rather than the name of your variable?
 
Upvote 0
Why can't you pass the actual Dictionary object to the procedure, rather than the name of your variable?

Hi Rory,
Thank you for review and offering a suggestion. I have applied the concept and can successfully pass an object.

I've come to the conclusion that I want to keep all dictionary methods within the Function itself (including the elusive "instantiate dictionary"). I continue to explore the concept of passing a string and converting it to an object without success such as use of CObj.


Do you know of any way that I can convert a string to an object, or programmatically create a string that can then be executed real time?



Thanks again for your consideration.

David
 
Upvote 0
The dictionary object doesn't have a name and I have no real idea what it would mean to convert a String to an Object.
 
Upvote 0

Forum statistics

Threads
1,216,375
Messages
6,130,243
Members
449,568
Latest member
mwl_y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top