Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Looking up items in HashTable/Dictionary objects that have multiple keys

, 1 May 2008
Dictionary objects take a single key as a look up key. This class simplifies using a Dictionary when you have multiple keys, such as two strings and an int, etc.
classkeydemo.zip
ClassKeyDemo
ClassKeyDemo
bin
Debug
ClassKeyDemo.dll
Properties
Test
bin
Debug
ClassKeyDemo.dll
nunit.framework.dll
Test.dll
Properties
classkeydemoallmethods.zip
ClassKeyDemo.dll
ClassKeyDemo.dll
Test.dll
classkeydemoallmethodswithtuple.zip
ClassKeyDemoAllmethods
ClassKeyDemo
ClassKeyDemo
bin
Debug
ClassKeyDemo.dll
ClassKeyDemo.csproj.user
Properties
Test
bin
Debug
ClassKeyDemo.dll
Test.dll
Properties
Test.csproj.user
classkeydemowithstruct.zip
ClassKeyDemo.dll
ClassKeyDemo.dll
Test.dll
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using Utility;
using System.Diagnostics;

namespace Testing
{
    [TestFixture]
    public class TestTestClassKeyStruct
    {
        /// <summary>
        /// Define test class to use key for
        /// </summary>
        public class TestClass
        {
            public string Column1 = null;
            public string Column2 = null;

            public TestClass(string Column1, string Column2)
            {
                this.Column1 = Column1;
                this.Column2 = Column2;
            }
        }

        public void TestClassKeyCompare(string Column1Orig, string Column2Orig, string Column1New, string Column2New)
        {
            TestClass model1 = new TestClass(Column1Orig, Column2Orig);
            TestClass model2 = new TestClass(Column1Orig, Column2Orig);

            Assert.AreEqual(new ClassKeyStruct(new object[] { model1.Column1, model1.Column2 }), new ClassKeyStruct(new object[] { model2.Column1, model2.Column2 }));
            Assert.IsTrue(new ClassKeyStruct(new object[] { model1.Column1, model1.Column2 }) == new ClassKeyStruct(new object[] { model2.Column1, model2.Column2 }));

            //change side of one and make sure not equal
            model1.Column1 = Column1New;
            model2.Column2 = Column2New;

            Assert.AreNotEqual(new ClassKeyStruct(new object[] { model1.Column1, model1.Column2 }), new ClassKeyStruct(new object[] { model2.Column1, model2.Column2 }));
            Assert.IsTrue(new ClassKeyStruct(new object[] { model1.Column1, model1.Column2 }) != new ClassKeyStruct(new object[] { model2.Column1, model2.Column2 }));
        }

        [Test]
        public void TestClassKeyDifferent()
        {
            TestClassKeyCompare(
                "one",
                "two",

                "two",
                "on"
            );
        }

        [Test]
        public void TestClassKeyXOROrderProblem()
        {
            TestClassKeyCompare(
                "one",
                "two",

                "two",
                "one"
            );
        }

        [Test]
        public void TestClassKeyNullProblem()
        {
            TestClassKeyCompare(
                null,
                "two",

                "two",
                "one"
            );
        }

        public class PerfResults { public long initMS; public long lookupMS; }

        public PerfResults GetTestPerf()
        {
            Dictionary<ClassKeyStruct, TestClass> testLookups = new Dictionary<ClassKeyStruct, TestClass>();
            Stopwatch stopWatch = new Stopwatch();
            PerfResults results = new PerfResults();

            //test initialization
            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
                testLookups.Add(new ClassKeyStruct(new object[] { testClass.Column1, testClass.Column2 }), testClass);
            }
            stopWatch.Stop();

            results.initMS = stopWatch.ElapsedMilliseconds;

            //test getting
            stopWatch = new Stopwatch();
            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
                if (!testLookups.ContainsKey(new ClassKeyStruct(new object[] { testClass.Column1, testClass.Column2 })))
                    throw new ArgumentException("Can't find object we know is there");
            }
            stopWatch.Stop();

            results.lookupMS = stopWatch.ElapsedMilliseconds;

            return results;
        }

        [Test]
        public void TestPerf()
        {
            int testRuns = 10;
            PerfResults totalResults = new PerfResults();

            for (int i = 0; i < testRuns; i++)
            {
                PerfResults thisResult = GetTestPerf();
                totalResults.initMS += thisResult.initMS;
                totalResults.lookupMS += thisResult.lookupMS;
            }

            Debug.WriteLine("Initialization: " + (totalResults.initMS / testRuns).ToString("N0") + "ms");
            Debug.WriteLine("Lookups: " + (totalResults.lookupMS / testRuns).ToString("N0") + "ms");
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul B.

United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 1 May 2008
Article Copyright 2008 by Paul B.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid