G+_Razelda Peacecraft Posted March 4, 2014 Share Posted March 4, 2014 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 More sharing options...
G+_Razelda Peacecraft Posted March 4, 2014 Author Share Posted March 4, 2014 I know I'm real bad about writing comments to show what part of the app is doing what Link to comment Share on other sites More sharing options...
G+_Joe Maruschek Posted March 4, 2014 Share Posted March 4, 2014 Square Iguana has the right idea: you can use global variables. You just need to declare them outside your Main() procedure. See this example: https://github.com/joemarus/Test1/blob/master/GlobalVars.cs Link to comment Share on other sites More sharing options...
G+_Razelda Peacecraft Posted March 4, 2014 Author Share Posted March 4, 2014 seeing that before Writing this code would saved me ton of time ... so now to just clean the code and mabe do what I whanted to more with it lmao Link to comment Share on other sites More sharing options...
G+_Louis Maresca Posted March 7, 2014 Share Posted March 7, 2014 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 More sharing options...
Recommended Posts