Dictionary Class Question.

Hi,
I have an atypical question:

For what do user’s use the dictionary class for?
I am now learning a different aspect of Xojo, and understand what a dictionary does (stores name / value pairs), but what are typical uses of such a class?

I would imagine I am missing out by not using this?
Is it normally just used for saving app’s settings etc - or are there lots of other practical uses for such a class?

Thanks.

we use them internally in places where a really fast lookup table is required

I have no idea what a lookup table is - so I am guessing I am not missing out by not using one :slight_smile:

Simply when you need to find a value based upon another value. A dictionary is much faster than iterating over a list to find the designated value. One such use is to convert a decimal ( i. e. 2) to its equivalent text (“two”). The Key would be “1” and the value would be “two”.

[code]dim D as Dictionary
D.Value(1) = “One”
D.Value(2) = “Two”

if D.hasKey(1) Then
myText.Text = D.Value(1).StringValue
end if[/code]

Roger,
So basically, a dictionary would be suitable for a translation type app?
Any other regular uses, which come to mind?

Thanks.

Just a quick related question.
If I had the following code:

[code]dim D as Dictionary

D.Value(1) = “One”
D.Value(2) = “Two”[/code]

How would I check to see if text entered into a text field matches a record stored in the dictionary?
Can’t find any reference on how to compare entered strings against the dictionary, in the Language Reference :frowning:

Would it be something like this:

if D.hasKey(myTextField.text) Then MsgBox("Found") else MsgBox("Not Found") end if

Ah its not a “spelling dictionary”

its whats known as a “hash table”

you have a key and from that you can very quickly get to the value associated with that key

???
I’m a bit lost - are you saying you can’t check an entered text string against a dictionary value?

oh you could
but thats not what “dictionary” in the language means
its a lookup table
to use it as a spelling checker you’d have to load it with all the words you want to spell
something like

[code] dim myspellings as new dictionary

myspellings(“hello”) = true // the VALUE in this case doesn’t matter all we need is some value
myspellings(“world”) = true
…
[/code]

and so on to load all the words you want to spell
now to check spelling you would do something like

[code]

if mySpellings.HasKey( userEnteredWord ) then
// ok they spelled it ok
else
// not spelled ok
end if[/code]

However there are some things you can’t do very well - like proposing words that might be the one that the user meant to spell
All a Xojo Dictionary can tell you is “i have that key or not”

for spelling, especially for suggesting alternatives, there are much better data structures

Thanks for the explanation Norm.
I wasn’t intending for it to be used as a spelling dictionary as such - I meant for example:

[code]Dim translations as New Dictionary
translations.Value(yes) = “Ja”
translations.Value(No) = “Nein”

if translation.hasKey(myTextField.text) Then
MsgBox (SOMEHOW SHOW THE CORRESPONDING GERMAN WORD HERE???)
else
MsgBox (“German Word Not Found in Dictionary”)
end if[/code]

The above intention was an example only - but I guess a normal database and queries would be more suitable?

if translations.hasKey(myTextField.text) Then MsgBox translations.Value(myTextField.text) else MsgBox ("German Word Not Found in Dictionary") end if

or in one line with Lookup()…

  MsgBox translations.Lookup(myTextField.text, "German Word Not Found in Dictionary")  

I had that question for a long time.
One use. I have for Dictionary is to send user information from my application to my use data base on my server. The server has a PHP program that add the record to the data base. I’m not sure but think this maybe explained in HTTPSocket, under ‘send’ and ‘post’.

Thank you very much Will - I will try that tomorrow :slight_smile:

Thanks Jim :slight_smile:

[quote=99757:@Richard Summers]Thanks for the explanation Norm.
I wasn’t intending for it to be used as a spelling dictionary as such - I meant for example:

[code]Dim translations as New Dictionary
translations.Value(yes) = “Ja”
translations.Value(No) = “Nein”

if translation.hasKey(myTextField.text) Then
MsgBox (SOMEHOW SHOW THE CORRESPONDING GERMAN WORD HERE???)
else
MsgBox (“German Word Not Found in Dictionary”)
end if[/code]

The above intention was an example only - but I guess a normal database and queries would be more suitable?[/quote]

You could do that that way
But I’d guess you’d want several languages and so you’d have to have some way to find the right thing in the right language

We use it in the IDE to cache references to the items in your project side we can look them up really rapidly
So when you create a new project item we stick it in the dictionary and then when ever there’s a references to it we can find it very rapidly
We have several dictionaries so we can find it by name, by id etc
And thats way faster than how we used to do it :slight_smile:

Cheers Matey :slight_smile:

I use dictionary to save the records from a recordset. I put all the values for each record into a class object and save it to the dictionary keyed by the primary ID value.

If you ever want to venture to the web edition side, learning to use the Dictionary is almost a must when it comes to session control. Learn it, it’ll be highly beneficial for you at multiple points in the future.

Communication between a thread and its UI-accessing part like in the task class (see examples folder) is most elegantly done via a dictionary. The thread calculates its results, puts them in a dictionary and then calls the UI-safe method (via a short period timer) with the dictionary as parameter.

In one case, I realized I needed to push over a varying number of results for one value (in form of an array of strings) which should be displayed on a listbox but needed to shove over additional information (an array of Addressbookrecords) which should not be displayed.
Conventional way would be to use two arrays for both data types.

In a dictionary holding variants, you can mix data types:
As a dictionary uses a key/value-pair, I simply put the output to display (the array of strings) in the value belonging to key x (being an integer counter) and the additional data (an addressbookrecord or an array of it) into the value of key x * –1. Don’t know if this is an ideal solution but the result is fairly fast compared to the conventional way mentioned above.