Using a dictionary in Xojo

Given a 400 x 400 array and a Gaussian-distributed pseudorandom number, Normal, is the following correct? I’m just learning how to program and would appreciate some feedback. Please see comments in code.

Dim settings As New Dictionary For i=0 to 399 For j=0 to 399 if settings.HasKey(Normal(i,j)) then // check in dictionary to see if a number has been previously returned for the i,j parameters Return settings.Value(Normal(i,j)) // if so, return that same number else ... Next Next

This is correct if you are trying to return the first value in the array that appears in your settings Dictionary. Won’t this keep returning the same value though?

It won’t ever return anything. The Dictionary is EMPTY as it was instantiated on the first line of the method

D’oh!

Thanks for telling me! So how do I correct it?

What exactly are you trying to do?

You correct it by creating the dictionary somewhere elses (global)

But how are you POPULATING it.
The code you posted looks for a key…

But what PUTS THE KEY and DATA IN?

Thanks for the replies. I would like to have a different normal-distributed pseudorandom number, Normal, at each site in the array given by i and j, but that number needs to be the same in function 1 as in function 2 for the particular choice of i and j, if that makes sense. I was advised to create a function that accepts the i,j parameters, returns the “normal” random number, and stores the number in a dictionary. The steps are:

-check in dictionary if a number has been previously returned for the i,j parameters
-if so, return that same number
-if not, generate a new number, store it and return it

Right now I’m stuck on implementing the first two steps.

// global at module level
Dim settings As New Dictionary
FUNCTION check_dictionary(i as integer, j as integer) as integer

  if NOT settings.HasKey(Normal(i,j)) then

     settings.value(normal(i,j))=new_random_number ' up to you to decide what goes here

end if

Return settings.Value(Normal(i,j)) // if so, return that same number

END FUNCTION

Thanks, Dave. How do I instantiate the dictionary at the module level? Do I just include it as a methods? Also, will this work if the function is a type double rather than an integer?

Add it as a PUBLIC property to any module in your app

I added it as a property called “settings” of type double under the “Globals” module, under which I wrote “Dim settings As New Dictionary”. Is that how to do it? I get some error messages when I run the program, for example: “Globals.check_dictionary, line 1: Cannot get this property’s value” etc.

No…
why did you create a Settings as DOUBLE?

just create a GLOBAL (public) property called SETTINGS AS DICTIONARY

that is it

FYI… Dictionary deals with VARIANTS so it doesn’t care what datatype you are using… put in what you want… and it will return what you put in

Not quite it. In the App Open event, put settings = new Dictionary

oops… my bad… I did FORGET that step :slight_smile:

Thanks. I put “settings = new Dictionary” in the App Open Event Handler as Kem suggested, and made a global property settings As dictionary, but get errors when I run the program:

App.Open, line 1: Cannot assign a value to this property: settings=new Dictionary
Globals.check_dictionary, line 1: Cannot get this property’s value: if NOT settings.HasKey(Normal(i,j)) then
etc.

is the scope of SETTINGS in your module PUBLIC?

The scope is Global. It doesn’t give me the option of making it Public.

Global=Public (sorry same meaning… different languages :slight_smile: )