Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 9.5

COLUMN

A Two-Way Dictionary

Extending the Dictionary

Issue: 9.5 (July/August 2011)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 16,621
Starting Page Number: 63
Article Number: 9509
Resource File(s):

Download Icon 9509.zip Updated: 2011-07-05 10:42:18

Related Link(s): None

Excerpt of article text...

I've written in the past about how wonderful dictionary objects are for data storage. I use them all the time. I like them better than arrays because they're faster for data retrieval and because they are based upon the variant datatype, you can store anything inside them (even other dictionaries). But dictionaries aren't the end-all and sometimes you can extend them or enhance them to create new objects that are even more powerful.

For example, how about a SortedDictionary? It always keeps itself in sort order even as you add new contents. (I'll leave you to write that one.)

You can also create dictionary variations that are optimized for the specific kind of data you're working with. That's what we're going to do today, but in a generic way that allows us to reuse this new class in all sorts of programs.

I'm talking about a Two-Way Dictionary. A normal dictionary is a key-value setup: that means you need the key to find its value. For most situations that's fine. But what if you had the value but not the key? Wouldn't it be nice to pass either the key or the value and get back its opposite?

There are many situations where such a setup would be valuable. I've created them manually in several programs I've written, using two dictionaries and extra code to make sure both stay in sync. But now I've gone the extra step and written a class to handle everything behind the scenes.

Not For Every Situation

...End of Excerpt. Please purchase the magazine to read the full article.