Dictionary - a C++ template class to emulate VBScript's "Scripting.Dictionary" collection

Dictionary  is a C++ template designed to duplicate the functionality, and, for the most part, the syntax of the Scripting.Dictionary ActiveX object built into VBA and VBScript.  It derives from an STL map<> template, so any of the STL algorithms will also work with it.  All of it's member functions are inlined, so there's only a Dictionary.h file.  There is no Dictionary.cpp.

In the examples below, the left side are taken directly from Microsoft published documentation for the Scripting.Dictionary ActiveX.  The right side is the equivalent C++ code using the Dictionary<> template.  As you can see, the syntax is nearly identical, with the only significant different is that the data types of the dictionary must to predefined, and the C++ methods require parentheses where the VBScript methods don't.

'VBScript sample code from Add Method
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Dictionary<string, string>    d;
d.Add("a", "Athens");
d.Add("b", "Belgrade");
d.Add("c", "Cairo");

VBScript sample code from both Count property & Keys method
dim a
a = d.Keys
For i = 0 to d.Count -1
s = s & a(i) & "<BR>"

vector<string>    a;
string            s;
a = d.Keys();
for (int i = 0; i < d.Count(); i++)
        s = s + a[i] + '\n';
VBScript sample code from Items method
dim a
a = d.Items
For i = 0 to d.Count -1
   s = s & a(i) & "<BR>"

vector<string>    a = d.Items();
for (i = 0; i < d.Count(); i++)
   cout << a[i] << endl;   
VBScript sample code from Exists method
If d.Exists("c") Then
    msg = "Specified key exists."
    msg = "Specified key doesn't exist."
End If

if (d.Exists("c"))
   cout << "Key: 'c' Exists" << endl;
   cout << "Key: 'c' does not exist" << endl;
VBScript sample code from Remove method
(docs never explain the return value of Remove method)
a = d.Remove("b") 'Remove second pair

VBScript sample code from Key property
d.Key("c") = "d" 'Set key for "c" to "d".

d.Key("c") = "d";
VBScript sample code from RemoveAll method
(docs never explain the return value of RemoveAll method)
a = d.RemoveAll 'Clear the dictionary


Summary of Public Interface:

// Dictionary.h: interface/implementation for the Dictionary class.
// Dictionary is a templatized class which is a direct copy of
// the VBScript Scripting.Dictionary object.  All methods
// and properties are provided with a syntax that is either
// identical to the VBS implementation, or with only trivial
// differences (mainly, C++ member functions require parentheses
// where VBScript methods don't)
// Copyright 1998, James M. Curran, All Rights reserved.
//  May be used freely, provided copyright noticed remains.
//  May not be sold in source code form.
// email: JamesCurran@Mvps.org
//   WWW: http://www.NJTheater.Com/JamesCurran
template<typename KEY, typename ITEM>
class Dictionary  : std::map<KEY, ITEM>
	Dictionary() {};
	virtual ~Dictionary(){};
	void			Add(KEY k, ITEM i);
	bool 			Exists(KEY k) 	const;
	size_type 		Count() 	const;
	void 			Remove(KEY k);
	void 			RemoveAll();
	std::vector<KEY>	Keys() 		const;
	std::vector<ITEM>	Items()		const;
	DKH			Key(KEY k);
	DIH			Item(KEY k);
	DIH 			operator[](KEY k);
	DIH 			operator()(KEY k);
	friend std::ostream& 	operator<<(std::ostream& os, const DIH& dih);

Note:   Anything above the returns a DIH or DKH can appear on either the right or left side of an assignment,  (although Key() is a bit pointless on the right hand side).  eg:

    dict.Item("a") = "Apple";
    dict("a") = "Apple";
    dict["a"] = "Apple";

are all legal (and equivalent).

Demo is included with header file.  Just compile & link a source file with the following three lines:

#include "dictionary.h"
void main()	{ dictionary_test();	}



  • Little Problem... Help!

    Posted by Legacy on 05/11/2003 12:00am

    Originally posted by: Jorge

    Hello, this class is great!
    I had a project assigned and I wrote down all the code in VB and then just used this library to be able to translate it.

    But I have a problem... The problem is that I made a class that has a dictionary inside... And then I made a dictionary of that class. All works fine but I cannot do something like this:

    class MyClass{
    Dictionary<string, string> MyDict;

    Dictionary<string, MyClass> DictionaryOfMyClass

    void main(){
    MyClass Something;
    Something = DictionaryOfMyClass.Item("Key");

    It says that my class does not support the "=" operator. Why is this? I think it also happens if you declare a dictionary and you try to equal it to another dictionary:

    Dictionary <string, string> d1;
    d1.Add("key", "item");

    Dictionary <string, string> d2;
    d2 = d1;

    But when you have no Dictionary inside of the class it will let you do something like:

    class MyClass{
    std::vector<string> MyVector;

    Dictionary<string, MyClass> DictionaryOfMyClass

    void main(){
    MyClass Something;
    Something = DictionaryOfMyClass.Item("Key");

    In this case this will work perfectly... But is there a way to make this work on the previous examples? Please help me... I have to finish it for tuesday and I am desperate.

    Thanks... :)

  • This is not necessary!

    Posted by Legacy on 10/19/2002 12:00am

    Originally posted by: Wilson

    This is not necessary! This is just a rehash of standard map and MFC CMap serial of classes. This one does not support the following: Runtime Classes, Dynamic Creation, Object Diagnostics and Serialization (Persistence). It is better to use CMap...

  • Finally! A good database object exists!

    Posted by Legacy on 04/09/2001 12:00am

    Originally posted by: Darryl Agostinelli

    I'm glad someone has taken the time to post a nice database object like this one.


  • dictionary file

    Posted by Legacy on 02/09/2001 12:00am

    Originally posted by: JB

    maybe you can help me. i need a dictionary file for my program. i will be interacting with junior high students to help them with their sentence formation and i don't want to create an entire dictionary. i know there has to be a file out there somewhere. if you are aware of it please responsd to dcandman@aol.com and place DICTIONARY in the subject bar so i don't delete it (mistaking it for junk mail).



  • Very good job man!

    Posted by Legacy on 12/25/1999 12:00am

    Originally posted by: Matro

    Hey, really thank you! I was looking for a fast-to-use associative array and here it is, with style! :-))

  • C++ source code?

    Posted by Legacy on 05/16/1999 12:00am

    Originally posted by: Edward Chen

    hi can you send me some code about using the array pass elements in a function?  How to use array references
    to pass another function? please send me a e-mail.thank you!! 

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • As your content grows, it can be challenging to define the proper rules, regulations and policies that govern that content and ensure proper protection and compliance. Entrusting your content to Box – the only secure, modern content platform in the cloud – is the right first step. The next step is to evaluate your retention, disposition and defensible discovery needs and to establish the proper buy-in to satisfy all your needs. In this Information Governance webinar, learn how Box Governance can add …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date