Code Optimization Help Needed.

Hi,
I have the working code below, but at the moment there are only 4 options.

Eventually there may be hundreds of options, which will mean that using If Statements or Select Case will become impractical due to the ridiculously large volume of code needed.

Is there a better way to do this, which will involve much less code when there are hundreds of options?

[code]If st = “Gibbon” Then
HtmlViewer1.LoadURL(http://www.site1.com)

ElseIf st = “Chimpanzee” Then
HtmlViewer1.LoadURL(http://www.site2.com)

ElseIf st = “Gorilla” Then
HtmlViewer1.LoadURL(http://www.site3.com)

ElseIf st = “Orangutan” Then
HtmlViewer1.LoadURL(http://www.site4.com)

End If[/code]

Hopefully someone can help.
Thank you all in advance.

– Dictionary with the st values as keys and the urls as values.
– SQLite (in-memory).

Thanks Eli.
Just out of curiosity, are dictionaries reasonably good at handling say 400 entries (speed wise)?

Thanks.

Dictionaries are based on a hash so the lookup time is relatively unaffected by the number of items. Although Eli’s alternate suggestion of an in-memory SQLite database would work too, a Dictionary would be far less work, and would probably be faster (just speculating), although either would be, for all intents and purposes, instant.

Thanks Kem,
I have a simple question here - (please bear in mind I have never used a dictionary before) :slight_smile:

If I use this code - all seems ok:

Dim URLDict As New Dictionary URLDict.Value("Gibbon") = "I'm a Gibbon" URLDict.Value("Chimpanzee") = "I'm a Chimpanzee"

However, if I make a property in a module (Name = URLDict, Type = Dictionary), and then use this code:

URLDict.Value("Gibbon") = "I'm a Gibbon" URLDict.Value("Chimpanzee") = "I'm a Chimpanzee"

I get a NilObjectException on line 1?

Any idea what I have done wrong?
Thanks.

You need to create the dictionary with New first.

Ahhh, like this:

URLDict = New Dictionary URLDict.Value("Gibbon") = "I'm a Gibbon" URLDict.Value("Chimpanzee") = "I'm a Chimpanzee"

Thanks.

I’d load the key-value pairs from a text file and stuff them in the dictionary. Then you can easily add/modify them in the text file rather than having tons of Xojo dictionary code.

Marc,
Excellent idea! Are there any examples of how to load a txt file into a dictionary?
Thanks.

off the top of my head

[code]dim f as folderitem = getOpenFolderitem() // or however you want to locate the file
dim t as new TextinputStream(f)

// lets pretend its a simple tab delimited file
while t.eof <> true
dim s as string = t.readline
dim key as string = s.nthfield(chrb(9), 1)
dim value as string = s.nthfield(chrb(9), 2)
dict.value(key) = value
wend
[/code]

Thanks Norman, I’ll give that a try.
Much appreciated.