Global Dictionary in a window

I really hate it when I know I’m missing something simple but can’t seem to get a handle on it. Here’s the problem I’m having now.

I have an app that, from a menu selection, is opening a new window. In that window, I have a Dictionary property. In the Window’s Open event handler, I am trying to create and set the dictionary’s keys to a blank value for later manipulation. Even though the Dictionary is global to the window, if I don’t declare it as a New Dictionary in the open event, it doesn’t seem to be seen and I get a NilObjectException. If I declare it, it seems to set up fine. The problem is that it seems to go out of scope when I leave the Open event handler since other methods in the same window see it as nil.

I know global intrinsic properties such as integers and strings, etc., don’t need to be Dim’ed (or Var’ed if you prefer) before they can be used and can hold their values while within the window’s venue. So what am I missing with it being a Dictionary?

in a method of the window

dim myDictionary = new dictionary 

in the Open Event

myDictionary.value("xxx")="abc"

If you create the instance in the open event… .it is local to the open… if you create it in a method of the window, the it belows to the window itself.

[quote=459416:@Dave S]in a method of the window

dim myDictionary = new dictionary [/quote]
But that’s still local to the method. It goes out of scope when the method finishes.

A common error is to have a property of the window named “MyDictionary” and then put a line in the Open event

dim MyDictionary as New Dictionary

The problem is that this creates a local variable with the same name as the window property. It sets the local variable and leaves the window property Nil. The correct code in Open is simply

MyDictionary = New Dictionary

That sets the window property without creating a local variable.

[quote=459415:@Dale Arends]I really hate it when I know I’m missing something simple but can’t seem to get a handle on it. Here’s the problem I’m having now.

I have an app that, from a menu selection, is opening a new window. In that window, I have a Dictionary property. In the Window’s Open event handler, I am trying to create and set the dictionary’s keys to a blank value for later manipulation. Even though the Dictionary is global to the window, if I don’t declare it as a New Dictionary in the open event, it doesn’t seem to be seen and I get a NilObjectException. If I declare it, it seems to set up fine. The problem is that it seems to go out of scope when I leave the Open event handler since other methods in the same window see it as nil.

I know global intrinsic properties such as integers and strings, etc., don’t need to be Dim’ed (or Var’ed if you prefer) before they can be used and can hold their values while within the window’s venue. So what am I missing with it being a Dictionary?[/quote]

“global” usually refers to the whole app, not just a window. You want a property Local to a window level, for that, in the Navigator right click on the window, add to window, property. Create the dictionary in there and then do what tim said:

[quote=459420:@Tim Hare]The correct code in Open is simply

MyDictionary = New Dictionary
That sets the window property without creating a local variable.[/quote]

[quote=459420:@Tim Hare]The correct code in Open is simply

MyDictionary = New Dictionary

That sets the window property without creating a local variable.[/quote]
Thanks, Tim. I knew it was something simple and for all the other times I’ve done it correctly, I feel really stupid for not seeing it this time.