• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

problem : programming using global variables

 i require to use 9 to 10 global variables in my program.
i know that using too much of global vars is a bad programming practice. so is there an alternative for using such a style of programming.

thanx in advance
1 Solution

9 to 10 is not all that much.

Alternatives to that:
'I do not like this one
local variables passed as parameters each time you go to a new form.
'This is even worse:
One class wich is created and mantained thorughout all the project to store those values as property
'This is bad the same
Store data in a file on disk, and read (or write) it each time you need the values (or to update them)

Point is: your app should be developed with no (or small) use of global variables (I repeat: 9 or 10 is not that much). If you really need them, better have them than make some strange artifical code to workaround...
Ryan ChongCommented:
Use global vars when more that 1 form is using it, while declare it as local variable when only 1 form is using it. Try reuse the global variable if possible as you can to avoid memory waste.
Using 9 to 10 globals shouldn't be to much of a problem, however an alternative would be to simply pass the required variable through to functions byVal, this will ensure that if the variable is changed within the function, it will also be changed in the calling function, eg:

Private var as String

Private Function A ()

var = "Something"

Call B(var)

MsgBox var

End Function

Private Function B (ByVal aVariable)

aVariable = "Something Else"

End Function

The msgbox in Function A will print out "Something Else" as it has been returned from Function B

Hope this helps
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Well, I sometimes use more than 9, 10 global variables in a program.  I was taught not to use too many of them, but if it simplify the program, who is there to complain.  Also these days, memory is not a problem for computers.  So as long as these global variable are not huge, I guess it is OK.  Another thing is rather than allocating and deallocating memory all the time with local variable, it could be faster to use a single global variable.  Lots of people might disagree with me, but I think if something makes the program simpler, why not follow it?
Global variables...  If you have less than 50 your doing fine :-)

To cut down on the, dimension them inside the the form that is calling them, if you only have one form and no modules, global is fine for them all.

It's not a memory issue, it's a neatness of code thing.  Any variable that is ONLY used withon one sub should be dimensioned in that sub.  If it was a memory issue then arrays would bugger it up.  An array with 255 elements effectively takes up the same memory as 255 different variables.
There is one major draw back to using global variables in programs and that is that if there is a local variable with the same name then the local can get overwritten. These errors are a nightmare to track down as they usually happen at run time ,but don't always happen. Global variables are the trademark of sequential programming like C, when working with VB to get the best out of it you need to use object oriented programming i.e. classes.
ashish_arp, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
Question is PAQ'd and no points refunded.

Community Support Moderator @Experts Exchange

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now