-1

in my application i have notice that i have approximately 30 global variables. is it bad programming and batter way is to pass to variable using the function or it doesn't matter ?

this is the list of all my globat variables from public partial class MainWin : Form

private const int WM_SYSCOMMAND = 0x112;
private const int SC_CONTEXTHELP = 0xf180;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
NetworkAdapter selectedAdapter = null;
string lastPath = "";
int _selectedIndex;
bool bContinuePlay;
bool ifContinue = true;
decimal delay = 10;
int delayBetweenLoops;
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
BackgroundWorker backGroundWorker = null;
bool isBurst = true;
IpV4Address oldIpAddress;
IpV4Address newIpAddress;
ushort oldPort;
ushort newPort;
MacAddress oldMacAddress;
MacAddress newMacAddress;
bool fixBadChecksum = false;
bool removePPPOE = false;
bool removeVlan = false;
bool fragmentation = false;
private DateTime lastCheck = DateTime.MinValue;
bool continuePlay = true;
RangeFinder range = null;
IpV4Address oldRangeIp;
IPAddress newRangeIpStart;
int loopsCount;
decimal numberOfLoops;
double playSpeed;
string path = "";
bool isError = false;
user2214609
  • 4,713
  • 9
  • 34
  • 41

4 Answers4

4

Move the variables to classes where they are used. You're not telling me that bContinuePlay, oldIpAddress, and path have anything to do with each other.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

It depends on the program. If all your code is one main function, why not. If it is a huge system, of course it would not be a good design, as it would later become messy.

Probably worth reading: http://c2.com/cgi/wiki?GlobalVariablesAreBad

naivists
  • 32,681
  • 5
  • 61
  • 85
0

A global variable is usefull only when you need it in many functions in the program, and passing it would make the parameter list too long.

It is bad practice however, to use globals as a one time variable.

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
-1

yes. Try to avoid global, they are no good. You can use static members of utility class, or pass input and output parameters, or class members.

Java for example doesn't have standalone parameters, everything is a class or belongs to your example I would create a class holding all the configuration.

something like

class GameConfiguration and maybe add static method to it (if you do not have multithreading that is)..

Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • -1 `You can use static members of utility class`. Those are globals too, but in different classes. – jgauffin May 20 '13 at 16:49
  • They are contained and do not pollute the global namespace.. – Dory Zidon May 20 '13 at 16:50
  • why did you -1? jgauffin?? Globals are ok at times, and even then do not just chuck them in the app.. – Dory Zidon May 20 '13 at 16:52
  • The OP has a LOT of globals, just moving them to different classes doesn't solve the problem. He need to encapsulate them in classes (making them non-static) and he ***might*** need a static property to access one or more of the newly created classes. But just saying that he should move them to new classes doesn't really help. – jgauffin May 20 '13 at 16:54
  • statics are just functional programming with lipstick. They typically prevent the goodies with OOP and makes it harder to maintain applications (due to increased coupling) – jgauffin May 20 '13 at 16:57
  • first I answered in regards to are globals a good idea. He should also use comments next to the objects he defines and his program looks very messy, however his question was, is it ok to use globals.. – Dory Zidon May 20 '13 at 16:57