Global variables are not supported in C#
-
Hi Richard. I'm going to try the get/set and other methods to find a way around this. I thought I had sent this on the C# forum....my mistake. Brian
-
The use of get/set is to hide the actual variables from the outside users, it has nothing to do with global variables. What you really should be doing is finding how to move all the variables inside the classes that require them.
Hi Richard. I started by using procedures but then found that I was adding a lot of variables to the procedure. Here is an example public void GameUpdate(int Score, int PlayerHealth, int PlayerLoad, String [] RoomName, String [] RoomDescription, int CurrentRoom, String [] ObjectName, int [] ObjectLoc, int GameCounter, int numObjects) I then tried a better way so all I had to do was call the procedure as I needed to call this procedure several times during the game. public void GameUpdate() but as Global variables are not possible then I may have to look at using classes. My variables such as RoomName needs to be used in several classes so if I was to use Inventory ADV = new Inventory; ADV.ObjectLocation = 3; than as far as I know ADV can only be used in the Inventory class and not other classes. Brian
-
Hi Richard. I started by using procedures but then found that I was adding a lot of variables to the procedure. Here is an example public void GameUpdate(int Score, int PlayerHealth, int PlayerLoad, String [] RoomName, String [] RoomDescription, int CurrentRoom, String [] ObjectName, int [] ObjectLoc, int GameCounter, int numObjects) I then tried a better way so all I had to do was call the procedure as I needed to call this procedure several times during the game. public void GameUpdate() but as Global variables are not possible then I may have to look at using classes. My variables such as RoomName needs to be used in several classes so if I was to use Inventory ADV = new Inventory; ADV.ObjectLocation = 3; than as far as I know ADV can only be used in the Inventory class and not other classes. Brian
-
If you need to pass that many variables then you need to look at the design of your classes. For example, you have a number of variables referring to a Room, so why not just pass a Room object that contains all those values?
Hi Richard. In your reply you wrote If you need to pass that many variables then you need to look at the design of your classes. For example, you have a number of variables referring to a Room, so why not just pass a Room object that contains all those values? Can you please provide a quick example to make sure that I fully understand your suggestion. Brian
-
Hi Richard. In your reply you wrote If you need to pass that many variables then you need to look at the design of your classes. For example, you have a number of variables referring to a Room, so why not just pass a Room object that contains all those values? Can you please provide a quick example to make sure that I fully understand your suggestion. Brian
As I have mentioned a number of times, you need to study some good learning materials and reference guides, for example, the two publications I previously recommended. Object Oriented Programming is all about the use of classes to encapsulate the properties and methods required to manipulate an object. If you still don't fully understand that then you will struggle with any programming task.
-
As I have mentioned a number of times, you need to study some good learning materials and reference guides, for example, the two publications I previously recommended. Object Oriented Programming is all about the use of classes to encapsulate the properties and methods required to manipulate an object. If you still don't fully understand that then you will struggle with any programming task.
I know what your saying Richard. Some books tend to get out of date for example I was reading a Microsoft book on C# that keep referring to code. To see the code they were referring to I needed to go to the web page printed in the book. The problem is that the web page no longer exists. I have managed to find useful articles on C# on the internet by typing questions into Google. Some internet Tutors are good but only if the tutor is good at teaching as some tend to skip over important things and teach at a fast speed. I find myself having to write down the code they are teaching. The best teachings I've found in the past is those that make it fun to learn such as building a project step-by-step which could be a game and having diagrams that show how things flow. Brian
-
As I have mentioned a number of times, you need to study some good learning materials and reference guides, for example, the two publications I previously recommended. Object Oriented Programming is all about the use of classes to encapsulate the properties and methods required to manipulate an object. If you still don't fully understand that then you will struggle with any programming task.
Although nothing like learning by experience though. I can remember quite a few messed up code messes I made much better than the books I read that told me not to do it like that in the first place (and I suspect the author is doing this for himself, so only he needs to maintain it.)
-
I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net. The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class. I'll need to experiment a bit to see if there is anyway around this. Brian
When I absolutely need global variables in C# or Java, I simply create a normal class I name it lobal
public Globa
l with the variables and include it in all the other classes. simple enough!CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
You are right, you cannot have global variables, and for good reasons. If a procedure needs access to variables then they should either be declared at the class level (and accessed through the get/set mechanism), or passed in from the calling process(es). Don't try to work round this by hacks, you will just end up down a blind alley, or worse, a buggy application. Also, please use the correct forum for your questions; this one is for Java not C#.
Richard, I agree with you in principle, but there are times where Global values are present, fo r example when I need units conversion factors, I like to declare them globally so they have a name, rather than coding the numbers directly into the equations and leave future observers confused at why 36000 is in the calculation, it;s better to write Secondsperhour instead of 3600 as a magic number for future readability and the seconds per hour won't change ever!
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
Hi Richard. In your reply you wrote If you need to pass that many variables then you need to look at the design of your classes. For example, you have a number of variables referring to a Room, so why not just pass a Room object that contains all those values? Can you please provide a quick example to make sure that I fully understand your suggestion. Brian
yeah! its work :sigh: :sigh: :sigh: