[Dictionary] how do I get the Value

I was trying to code something and I ended writing the code below.

The idea is to get both parts of each entries of the Dictionary.

I set only one entry in the Music_Disc (below) using the band name and the first names of each members of the band.

I am able to get the Key (aKey below), but not the Value (aValue below). I tried with Music_Dict.Lookup(Search, "Default_Answer"), but I get “Default answer”.
Using Music_Dict.Value("String") or Music_Dict.Value(Search_Variant) leads to KeyNotFoundException.

I am out of ideas. Do you have some (the good one, of course) ?

[code] Dim Music_Dict As New Dictionary
Dim aValue As Variant // Computed, I await the name of the band (The Beatles)
Dim aKey As Variant // Computed, I get: John, Paul, George, Ringo
Dim The_Group As Variant // Holds: The Beatles
Dim The_Kids As Variant // Holds: John, Paul, George, Ringo

// Fills the names
The_Kids = “John, Paul, George, Ringo”
The_Group = “The Beatles”

// Add an entry in the Dictionary
Music_Dict.Value(The_Group) = The_Kids

// Get the Key [Contents of The_Kids]
aKey = Music_Dict.Value(The_Group) // Returns: “John, Paul, George, Ringo”: OK *

// Get the Value [Contents of The_Group]
// How do I do that ?
// aValue = I do not know how to code that line to get “The Beatles”[/code]

Passing a string instead of a variant (“The Beatles” instead of The_Group) returns the same contents: OK.

Music_Dict.Value(The_Group) = The_Kids

The_group is the key.

“John, Paul, George, Ringo” is the value of the key

Many things could have the same value, so getting ‘from’ a value back to the key is not a great idea.
However, you could loop through the keys until you find one that has the value you are looking for

for each thekey as variant in Music_Dict.keys if Music_Dict.value(thekey) = The_Kids then msgbox thekey end if next

Hi Jeff,

thank you for your answer.

I am completely lost.

I try to synthetize my idea:

I store two different values in each dictionary entries.
One is the Key, the second is the value.

When I have one of these I want to get the other.
Also, when I have the other, I want the one.

Is that more clear ?

BTW: I may be asking the Moon (a.k.a.: this is not possible). If so, I may use a Collection or a Pair ?

After all these tries (nearly three hours ! I feel ashamed), I forgot why I wanted to do that; I created a sample to discover the way to do what I want to do. Usually, this works fine, but not this time.


In a dictionary you can only look up using the key, not the value. If you need to look things up by the value, you can create a second dictionary for the reverse lookup.

Dictionary1 : key=key, value = value.
Dictionary2: key=value, value = key.

You need to know the key before you can look up the value. If you have a dictionary and want to know what you previously put in it you can use Music_Dict.Values() or Music_dict.Keys().

OK folks, thanks.

I will investigate Pair (I think) and I hope I will find why I was going that way. Probably in a recent project lies the reason why I create this project.

The problem with reverse lookup is this:

[code] The_Kids = “John, Paul, George, Ringo”
The_Group = “The Beatles”

// Add an entry in the Dictionary
Music_Dict.Value(The_Group) = The_Kids
Music_Dict.Value(“Banana”) = The_Kids
Music_Dict.Value(“Tiger”) = The_Kids

//NOW… what is the key associated with The_Kids???
//There are 3 of them[/code]

Maybe a better method is to maintain a database table

[code]//using a table defined with 2 columns, theKey and theValue

insert into myPairs VALUES (‘The Beatles’,‘John, Paul, George, Ringo’);
insert into myPairs VALUES (‘The Who’,‘Roger,John,Keith,Pete’);
insert into myPairs VALUES (‘The Battles’,‘John, Paul, George, Ringo’);

//get a value

select theValue from myPairs where theKey = ‘The Who’;

//get a key

select theKey from myPairs where theValue = ‘John, Paul, George, Ringo’;
//this returns TWO rows

so maybe:

select max(theKey) from myPairs where theValue = ‘John, Paul, George, Ringo’;


I love the idea. I will explorate it seriously tomorrow (after checking the project where I needed that).

Hey folks, it Saturday ! Where is the fever ?

Here is a two-way dictionary in Swift that I’ve used - maybe you can port it to Xojo. https://gist.github.com/CanTheAlmighty/70b3bf66eb1f2a5cee28

Thanks Julia.

Happy Christmas to everybody…