.NET Tips and Tricks

Blog archive

Accessing a Dictionary Using Multiple Keys

Dictionaries and some other collection objects allow you to store an item along with a key value. This allows you to retrieve the item later, either by position or by key value. This code stores a value under the key "PHVIS," and finishes by setting the variable Result to "Peter Vogel":

Dim Items As New Dictionary(Of String,  String)
Items.Add("PHVIS", "Peter  Vogel")
Dim Result As String
Result = Items("PHVIS")  

But what if you want to store something by two values -- storing sales amounts by CustomerName and SaleDate, for instance? You could just concatenate two values together to create a single key, but a better solution would be to define a class to hold the two values to use as the key. Unfortunately, using a class won't work. When testing for equality, these collections will use an object's hashcode, returned from the class' GetHashCode method, to determine if two objects are identical. Two different objects, even if they're from the same class and have identical values in their properties, are considered to be different under this test.

But if you use a Structure instead,, everything works. Structures are scalar types rather than reference types like objects, so their values are compared to determine equality. A structure to hold two values might look like this:

Structure TwoValue
  Public CustomerId As String
  Public SaleDate As DateTime
End Structure

To use this structure, you might store a customer's sales under the customer's identifier, and the date that the sale was made:

Dim Items As New Dictionary(Of TwoValue, Integer)
Dim Key As TwoValue
Key.CustomerId = "A123"
Key.SaleDate =  Date.Parse("11/01/2010")
Items.Add(Key, 132)

Retrieving the value just consists of assembling a matching key and passing the key to the Dictionary. This example will retrieve the value and puts it in the variable Result: 

Dim Result As Integer
Dim Key2 As TwoValue
Key2.CustomerId = "A123"
Key2.SaleDate =  Date.Parse("11/01/2010")

Result = items(Key2)

Posted by Peter Vogel on 04/04/2012


comments powered by Disqus

Featured

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube