problem : programming using global variables

Posted on 2003-03-05
Medium Priority
Last Modified: 2010-04-07
 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
Question by:ashish_arp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8070373

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...
LVL 53

Expert Comment

by:Ryan Chong
ID: 8070379
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.

Expert Comment

ID: 8070439
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 8071426
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?

Expert Comment

ID: 8071488
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.

Expert Comment

ID: 8071538
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.
LVL 49

Expert Comment

ID: 9046914
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

Accepted Solution

YensidMod earned 0 total points
ID: 9151065
Question is PAQ'd and no points refunded.

Community Support Moderator @Experts Exchange

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question