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>"
Next

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>"
Next

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."
Else
    msg = "Specified key doesn't exist."
End If

if (d.Exists("c"))
   cout << "Key: 'c' Exists" << endl;
else
   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
 

d.Remove("b");
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
  
 

d.RemoveAll();

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>
{
public:
	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:

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


Downloads

Comments

  • 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{
    public:
    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{
    public:
    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... :)

    Reply
  • 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...

    Reply
  • 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.

    Thanks!

    Reply
  • 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).

    Thanx...

    JB

    Reply
  • 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! :-))

    Reply
  • 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!! 
    

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds