Jump to content

hay, This is only 3rd C# Program, I 've done a boat load of VB net program and Bash Scripting (...


G+_Razelda Peacecraft
 Share

Recommended Posts

hay,

This is only 3rd C# Program, I've done a boat load of VB.net program and Bash Scripting (i love linux) . there has to be an easyer way of Declaring the Varibals. In VB.net we just type: dim name As whatever. It covers that entier app as long as you know how to call it in other parts of the app. 

http://pastebin.com/80FgDzSG

Link to comment
Share on other sites

Personally I am not a fan of Global Variables. More on that soon… To those who are reading this: Global Variables are variables that are added outside the scope of any function or method making them accessible by all methods in a class. In a new Console Application the ‘Program’ Class can have global variables declared above the ‘Main(…)’ method. Since the Main(…) method is ‘Static’ that means the Global Variables also have to be Static as well.

Which brings to me to why I don’t like Global Variables, especially static ones:

 

Over the years I have seen people code using global variables in order to easily pass data around to different functions. The more global variables in a program the more confusing and complex the program can get. Knowing who is accessing those variables and worrying if the same variable is accessed at the same time from multiple different functions causing, what programmers call a, ‘race condition’, which means errors can happen indeterminately. For me, I use Global Variables as a sign that the program may need revisited on how it was designed. Since we really didn’t get to Classes, Objects and Abstraction in Coding 101 yet, I don’t want to go into too much detail. Normally when a program starts to look like Spaghetti (hard to follow a strand of spaghetti with a big bowl of pasta) with variables peppered all over the place causing complexity to increase, than the program is revisited\refactored breaking functionality into separate classes or abstraction layers simplifying the program as a whole.

 

As a suggestion to Global Variables, Think about having the functions pass state or data around. Or, another option would be to create a new class called ‘StateManager’ which can return data and state of the program. Creating an instance\object of StateManager in the Main (…) method in a Console can than be passed around to different methods. StateManager class can have a whole bunch of properties\attributes that store state, making it easier to pass it to methods.

 

I hope that helps!

 

By the way, using ‘var [variable] = [some Value] is just a fancy way of cleaning up an application when declaring variables. The application inferred the type the variable needs to be based on the initialization value. It’s not the same as ‘DIM’ in Vb.net. Normally VB.net ‘Dim’ is more like ‘Object [variableName] = [some value]’ in C#.

Link to comment
Share on other sites

 Share

×
×
  • Create New...