>a global var needing an accesor probably is a wrong >candidate to run for the position.
Agree.
>you can hide the var in the interface section and write a >shell function/procedure
You can, but creating an object make code clearer and forces you to think positively about program structure.
Global variables are perfectly legit, but experience shows that programs with lots of global vars tend to be bug ridden. I recommend using them only when there really is no reasonable alternative.
Lets take some examples.
1. Most Delphi programs have a global var referencing the main form. Fine.
2. Delphi has global vars for Printer, Screen and a few others. These are references to physical things of which there are only one. (All right multiple printers, but a TPrinter is a singel access point to all of them). Works well.
3. Most of my apps have a global var referencing a data module which provides the single point of access to the database. Again, works well.
4. Many apps need some global to specify what authorities the current user has, often an array or set. I say make it a property of the global data module, not a global in its own right. It can then reference an array today but upgrade to a dataset tomorrow.
5. Status. I have often found programmers using global vars to control the status of something, justified with the claim that there is only ever one of "something". Bad news. Why?
- soon there will be 100+ global vars, with it being virtually impossible to trace when each one is updated or is valid (I've seen it)
- When your app only had three forms the purpose of "Status" was clear. Now it has 20 forms, which one did "Status" belong to? "Opps..........Status is being referenced by CustomerEdit and OrderEdit"
- Next year your customer will want two concurrent instances of "something". Oh dear, complete re-write needed.
So much for what not to do, what about what to do?
Create a helper object, containing all the data that multiple objects need and pass a reference to it to all relevant objects. That way you are in control of when the instances are created and destroyed and how many instances there are.
I'll write an example as another post. It will look long, but its actually simple. OOP tends to involve a lot of typing up front. Most of it is simple and you get less debugging later one. [I wish I hadn't started this now <g>]