Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Global variables are not supported in C#

Global variables are not supported in C#

Scheduled Pinned Locked Moved Java
csharpc++python
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Brian_TheLion

    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

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    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.

    B 1 Reply Last reply
    0
    • L Lost User

      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.

      B Offline
      B Offline
      Brian_TheLion
      wrote on last edited by
      #7

      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

      L 1 Reply Last reply
      0
      • B Brian_TheLion

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        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?

        B 1 Reply Last reply
        0
        • L Lost User

          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?

          B Offline
          B Offline
          Brian_TheLion
          wrote on last edited by
          #9

          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

          L M 2 Replies Last reply
          0
          • B Brian_TheLion

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #10

            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.

            B J 2 Replies Last reply
            0
            • L Lost User

              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.

              B Offline
              B Offline
              Brian_TheLion
              wrote on last edited by
              #11

              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

              1 Reply Last reply
              0
              • L Lost User

                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.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #12

                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.)

                1 Reply Last reply
                0
                • B Brian_TheLion

                  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

                  D Offline
                  D Offline
                  Dr Walt Fair PE
                  wrote on last edited by
                  #13

                  When I absolutely need global variables in C# or Java, I simply create a normal class I name it lobalpublic Global 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

                  1 Reply Last reply
                  0
                  • L Lost User

                    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#.

                    D Offline
                    D Offline
                    Dr Walt Fair PE
                    wrote on last edited by
                    #14

                    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

                    1 Reply Last reply
                    0
                    • B Brian_TheLion

                      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

                      M Offline
                      M Offline
                      Member 14558895
                      wrote on last edited by
                      #15

                      yeah! its work :sigh: :sigh: :sigh:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups