Dictionary in Console Application

I am building a console application and trying to set key value pairs in a global dictionary (one that belongs to the APP thread) to hold application parameter values read from a file. I can open and read the file, but when I try to assign the key value pairs, I get a nil object exception error. Is there a problem with using a dictionary in the app class on a console application?
Even this fails as a test:

AppParameterDictionary.value(1)="!"

AppParameterDictionary is a dictionary property defined in the IDE

It sounds like you forgot to instantiate the Dictionary.

You talk so dirty … :slight_smile:

Yes, but how?-- normally when I make a property in the app class, like an array or string and give it a default value, I don’t have to Dim it in the code. Are you saying I should put something like this in the run thread of the app:

Dim AppParameterDictionary as new Dictionary?

Even if I have defined it as a property in the IDE? I want it to be a global property that all the methods are able to access. If it’s defined in the Run thread, won’t it be limited to the run thread?

A String isn’t an object, but a dictionary is. Good place to instantiate object properties is the constructor.

Add the dictionary (say myDict) as a global property by creating it in a module (say modGlobal). In the application open event before using the dictionary, write:

myDict = new Dictionary

it is now ready for use anywhere in your code as “myDict.” Don’t define it in the thread. Threads can access items (ie Dictionary) which exist outside them… just not GUI components.

@kem @mike - that made my day :-p

Anytime you use “dim”, you are creating a local variable even if the variable name is the same as a property. So if you did

dim AppParameterDictionary as new Dictionary

you will create a local variable with that name that will go away as soon as the method ends. The property you defined of the same name will never be accessed. So don’t do that. :slight_smile:

On the other hand, when you create a local variable that is meant to hold an object, like a Dictionary, you must instantiate it with “new”, right? Otherwise you’d get a NilObjectException when you tried to use it. Properties are exactly the same way. By creating the property, you’ve created a space for the object, same as if you’d done this.

dim AppParameterDictionary as Dictionary

If you now try to access it, as you have, you will get a NilObjectException, so before that point, you must create a new one:

AppParameterDictionary = new Dictionary

(I know I’m repeating what Matthew said above, but hope this clarification helps.)

And here’s a tip: When you create a property of which there will only ever be one that must be instantiated, do it as a computed property and only fill in the “Get” section. That code would look something like this:

Computed Property AppParameterDictionary
Get
  static d as Dictionary
  if d is nil then
    d = new Dictionary
  end if
  return d

That creates a property that instantiates itself. (You can research “singleton” for more information about this concept.)

HTH.

Thanks everyone for the input. I think I have it now.