|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Global (Public) variablesI'm working on an application that I've set up with several forms,
each to perform a particular set of functions. I'd like to have a few variables that are common to all forms, figuring that is the way to share the data among forms. 1. How do I make that work? 2. Is there a better approach to having some core data available from each form? 3. Is there a better way to organize the whole schmear? Sharp wrote:
> I'm working on an application that I've set up with several forms, I generally create a single public CGlobals class, instantiate it from Sub Main, and> each to perform a particular set of functions. I'd like to have a few > variables that are common to all forms, figuring that is the way to > share the data among forms. > 1. How do I make that work? > > 2. Is there a better approach to having some core data available from > each form? > > 3. Is there a better way to organize the whole schmear? expose properties from that. This has the advantage of encapsulating data retrieval and storage (from/to registry, INIs, whatever) for global data. "Sharp" <some***@somewhere.net> wrote in message There are quite a few ways... the most straightforward would be to add a news:lrpil1tdh1es7rugmftoqaa51i9lcodikp@4ax.com... > I'm working on an application that I've set up with several forms, > each to perform a particular set of functions. I'd like to have a few > variables that are common to all forms, figuring that is the way to > share the data among forms. > 1. How do I make that work? ..bas module to your app (Project/Add Module) and declare the variables Public in there. Anything declared Public in a .bas module is available to the entire app. > 2. Is there a better approach to having some core data available from If you're careful, you can get away with the globals. They can really get > each form? out of hand if you're not so careful. Since they're available to the whole app, they're easily "stomped" on. > 3. Is there a better way to organize the whole schmear? It depends on a lot of things. You can create a class that contains all of your "globals" and pass that around to who ever needs it, designate a specific form to be that "class", add public properties/variables to that form and treat it like a class, all kinds of different ways. The bottom line is.... declare variables with as little scope as possible. That way, finding code that manipulates those variables is much easier.... which means bugs are easier to find. fwiw, here's a tool that'll tell you if you have something declared with "Excessive Scope" (and about 1000 other details about your code) and show you what to do to fix that situation. It'll say something like "Move the variable named XXX to module YYY and change the declaration to Private". Very cool tool... I have one of the older versions. Project Analyzer v7.1 http://www.aivosto.com/project/project.html -- Ken Halter - MS-MVP-VB - http://www.vbsight.com DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm Please keep all discussions in the groups.. "Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message And usually significantly reduces the number of bugs caused by "stomping" on > The bottom line is.... declare variables with as little scope as possible. > That way, finding code that manipulates those variables is much easier.... > which means bugs are easier to find. a global variable. Yes, global variables have their purpose and can be beneficial IF you use them properly. In my experience of 14+ years using VB, I've learned that there is rarely a "genuine" need for a global variable. There are almost always alternatives that will make your code much better (tighter, encapsulated, etc.). You might have to write more code to avoid the use of the global variable (and in a worst-case scenario, even radically change the design of your program), but usually, you're better off. -- Mike Microsoft MVP Visual Basic
The Elements of Style revisited.
Intergating Multiple VB6.0 Projects into one: Project Grouping Parsing UNIX text files Return an error code/message from a VB6 app How to access related documents in VB through source code? Is this possible ? Please Explain! Lost_Focus event of controls doesn't fire Order of forms MSDN Library on network location |
|||||||||||||||||||||||