Dictionary as global variable

I’ve created a program that uses a method to perform some calculations. I am trying to setup a dictionary as a global variable so the method can enter/change the dictionary entries. I added a module (called GlobalVariables) to the app and then added a property (named Webster) to the module with the name of the dictionary and set it to global. The program compiles, but when the method is called, I get the NOE error when the method tries to add an entry to the dictionary. The dictionary is initially empty so the error occurs on the first attempt to add to it. Any help or example code would be appreciated.

Have you initialised the property, Webster? It will be Nil otherwise.

You need:

Webster = new dictionary

in the Opening event of your app.

And just like that…it works! Thanks Tim, your a life saver!

2 Likes

And you know why, by the way?
Putting a property somewhere doesn’t automatically create the object.

Thanks Arnaud. I assume I would need to make the same declaration statement in the app opening event for numbers, strings, etc. if I need them to be global?

Numbers and strings aren’t objects (to keep things simple). You can use integers or other non-objects types right out of the box, as this example shows:

Var b as boolean
Var c as color
Var i as integer
Var s as string

//Here, b=false, c=RGB(0,0,0) (=black), i=0 and s=“”.
//You can freely manipulate them:
b=true
c=RGB(0,255,255)
i=77
s=“Hello world!”

The NilObjectException error occurs only with objects (the ones that are nil until you create them):
Var d as DateTime
Var i as integer=d.Day //Exception: d is nil, so you can’t access anything from it (similar as if you asked “What’s the colour of this pencil that doesn’t exist?”).

But:
Var d as DateTime=DateTime.Now //d becomes non-nil
Var i as integer=d.Day //Works

Think of them that way:
Datatypes hold single, base values (numbers, booleans, strings, …).
Objects are made of datatypes properties (e.g. a window has left&top (integers), title (string) and closable (boolean) properties).
Objects can be nil; they eventually become something useable in code, after creation (with the “New” keyword, often). Accessing a nil object is like asking “How many legs have this animal that doesn’t exist?”, it doesn’t make sense and raises an exception.

HTH.

2 Likes

Definitely helps. Thanks for the insight.

1 Like

You’re welcome.