Dictionary are globals ? or not ?


I would like to create a global dictionary.
1 - I declared a global property into a module ( “myDictionary as dictionary” )
2 - I created a “Dim myDictionary as new Dictionary” into open event Windows
3 - But when I try to add a pair of values into myDictionary ( "myDictionary.value(“myKey”)=“red”) from an ‘action’ event (button), I generate a NilPbjectException error. Dictionary is not declared !

Could tell me why ? I don’t understand…

[quote=21267:@Dominik Fusina]Hi,
2 - I created a “Dim myDictionary as new Dictionary” into open event Windows

Change it to

myDictionary = new dictionary 

You got the global right, but in your open event, you’re creating a new local dictionary instance which is then released when the open event is complete.

Just so you understand, scope always works from local outward. So if you have a variable in a module with the same name as a local variable, the local one is what you’ll access when you type the variable name. If you wanted both, but wanted to access the shadowed variable, you’d need to enter the class/module you’re referencing. For example:

[code]Dim myDictionary As New Dictionary

myDictionary.Value(“Foo”) = “Bar” // Local
myModule.myDictionary.Value(“Foo”) = “Bar” // Module[/code]

In your module create a Computed Property called “myDictionary” and add this code to it:

Static dct As New Dictionary() Return dct

But don’t do that in a class unless every instance of the class is going to share the same Dictionary. That’s just an FYI.

i create a module called basDict and have a bunch of global dictionary properties which i populate on open of the application and then used it in my application for combo box. Is this a good way to do things??

I was doing this too, but switched to using classes that way I do not have to remember all the keys or worry about making a typo, plus I find it looks nicer too…

You can also use constants to reduce typos with the key names.

before i use dictionary, i was reading the data from the table everything i open the window with a bunch of combo box. Now i still need to do that… but getting the data from the dictionary. Is there a better way to make populating combobox faster??

Sam, i don’t think i can use constants in this case.

shoa sean, how do i use classes in my case??

I use constants for the key names, example, albeit added either to the class or module.

const backgroundColorAsComponentString = "backgroundColorAsComponentString"

So if this is for a particular control, I would set it in the class.


Even though it’s only a key and not the actual property. I’ve also created dictionary classes with computed properties as it helps with serialization.

i usually define my constant in a module call constant and then used it in other module