I started with this Dictionary Example and have been modifying the code. Everything works, but I am stuck on this one point.
The Dictionary is defined as a Property, listed on the left hand side Object Browser, in myModule. Within that Module I have a Load_Dictionary Method, which is passed a listbox, and then creates a dictionary entry for each row. This works fine! But I rather not have the property defined in the Object Browser. I like to Dim all variables within the Method where they are used. This makes it easy to copy and paste Methods between apps. Also, too many Properties listed in the Object Browser, makes it difficult to remember which Properties are used by which Method.
So Ive tried this again and again, in many ways. I removed the Object Browser Dictionary Property definition and replaced it with code like this, in my Load_Dictionary Method.
dim nameDict as new Xojo.Core.Dictionary
But then references to my Dictionary name are not recognized, generating and errors, This item does not exist.
Note: I did this with my SQLite database Property definition. I moved the database definition Property from the Object Browser into the Method where it was used. So Im thinking here too, there must be a way to define this Dictionary within the Method.
when you use DIM in a method then that variable does NOT exist outside that method
and so nothing else can refer to it (other methods cannot look inside each other for variables)
this is SCOPING in action
understanding scoping is crucial to being successful in just about every programming language I can think of
its covered in the introduction to programming with xojo guide
That’s for your quick reply Norm. Yes. I understand scoping.
But that’s something inherent in app architecture. If I want my Dictionary to be available throughout my app then it needs to be defined as Global. I’m thinking, where a Property is defined should not be the determinant factor as to it’s scope. I have tried many variations of syntax for this definition, including…
dim nameDict as new global.Xojo.Core.Dictionary
Are you saying it’s not possible? Also, from your experience, isn’t it a good idea not to have to many Property definitions is the Object Browser?
Where are you adding the Property?
If you added the property to App, then you need to access that property as App.nameDict
[quote=439319:@paul townsend]That’s for your quick reply Norm. Yes. I understand scoping.
I would say that given what I wrote and what you’re trying that you dont understand it well enough
[quote=439319:@paul townsend]But that’s something inherent in app architecture. If I want my Dictionary to be available throughout my app then it needs to be defined as Global. I’m thinking, where a Property is defined should not be the determinant factor as to it’s scope. I have tried many variations of syntax for this definition, including…
WHERE it is defined IS what defines it scope and visibility to other parts of the application
This will be local to whatever place you have defined it
dim nameDict as new global.Xojo.Core.Dictionary // this one is local to foo and NOTHING else canEVER see it outside this method
for i as integer = 1 to 1
// this one is local to the for loop
// and NOTHING else can EVER see it outside this loop
dim otherDict as new global.Xojo.Core.Dictionary
// and fwiw the same is true for i as the "for i as integer" defines I locally to the loop
if i = 55 then // this will give you an error about i does not exist - it doesnt at this scope level
if otherdict is nil // this will give you an error about otherdict does not exist - it doesnt at this scope level
100% not possible unless the variable is defined at a level that other methods etc can see it - which means it cannot be INSIDE a method
Also, from your experience, isn’t it a good idea not to have to many Property definitions is the Object Browser?[/quote]
Makes no difference as far as speed etc
You define things at the narrowest scope level (ie dont make everything a global) they need to be so your code works as designed & expected
Norm: thanks for taking the time to set me straight on this!!