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. C#
  4. Looks like C# does not support Global Variables

Looks like C# does not support Global Variables

Scheduled Pinned Locked Moved C#
csharpc++javapython
30 Posts 5 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.
  • L Luc Pattyn

    X|

    Luc Pattyn [My Articles] Nil Volentibus Arduum

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

    Thanks Luc. Brian

    1 Reply Last reply
    0
    • B Brian_TheLion

      Hi Griff. I have come from a background of programming in Basic then Visual Basic, so I tend to slip into this type of programming. I understand that the class method is a more modern way to program. It's just getting my head around classes even when reading about them. Brian

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #22

      And it shows! Instead of trying to get your head round how C# and OOPs design works, you're trying to force the code into your older methodology - and that doesn't produce good code! Modern software thinks in terms of objects and instances, just the same as you do in the "real world". You wouldn't create an array of all Cars and try to move them all relative to each other in the real world, you would create a CarPark with a collection of Levels, each of which would have a collection of ParkingSpaces - your Car instances would then park in a free ParkingSpace without any reference to where the other cars are parked because it's irrelevant - all you want to know is "is this space free", you don't care if it's Car[0], or Car[1], or Car[n] that is in the space, do you? And that is exactly what you do in the real world: you enter the car park then drive through it looking for the first empty space. When you leave, you go to the level you parked on, then to the space (and hope your car is still there). You drive out of the car park, and you are in a different area: the high street, which connects to Millers Lane at one end and the Bypass at the other. So why would you care about Sesame Street, which is half way across town? The High St "knows" what it's connected to: CarPark, Millers Lane, Bypass. And so on. So the the whole topology of the town is arranged as a series of connected places: Sesame St, Bypass, Millers Lane, High Street, etc. - they only need to know what they are connected to because that "lays out" how the town is arranged. Think about it, read the books - and this time don't skip stuff because "I know this from Basic" but try and get your head around how Object Oriented Programming helps you to reflect the real world more easily!

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • B Brian_TheLion

        Yes I admit that I have always had problems in understanding classes and now and then I come across some code that makes it a bit easier to understand. I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command. Brian

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #23

        Member 14154627 wrote:

        I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command.

        No, a List<T> is a "self expanding array" and can be accessed in the same way as an array:

        List<string> items = new List<string>();
        items.Add("Hello");
        items.Add("World");
        Console.Writeline(items[0] + " " + items[1]);

        For reference there is this: List<T> - Is it really as efficient as you probably think?[^] - don't look at it too hard now, it may be a little more advanced than you need at the moment.

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        B 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Member 14154627 wrote:

          I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command.

          No, a List<T> is a "self expanding array" and can be accessed in the same way as an array:

          List<string> items = new List<string>();
          items.Add("Hello");
          items.Add("World");
          Console.Writeline(items[0] + " " + items[1]);

          For reference there is this: List<T> - Is it really as efficient as you probably think?[^] - don't look at it too hard now, it may be a little more advanced than you need at the moment.

          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

          Thanks Griff. I haven't used List before so I hope to try it out by typing some example code. Brian

          OriginalGriffO 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            The idea is that you don't need global variables in C#, and by and large it's true. You can simulate them using static :

            public static class Globals
            {
            public static int GlobalInteger = 666;
            }
            ...
            Console.WriteLine(Globals.GlobalInteger);

            but I've only done it with a handful of variables in all the C# code I've ever written. If you need lots of globals, it's normally a BIG sign that your whole design is wildly wrong. I'd seriously think about the whole structure of your app if I were you - it's sounding very much like you didn't read any of the books after all.

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #25

            Well, a lot of guys that come from the C/C++ world into C# really struggle with the memory management aspects of .Net. Like me. I have a static Globals class in pretty much every project I develop. Most of the stuff in my Globals classes are methods that don't really make sense to put into any other non-static class. Along with those methods, I have properties that only need to be set once in the program (many times, it's a complex object or list of objects), or that simplify access to system methods/vars (to ease typing elsewhere in the app).

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            1 Reply Last reply
            0
            • B Brian_TheLion

              Thanks Griff. I haven't used List before so I hope to try it out by typing some example code. Brian

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #26

              :sigh: Stop doing that. Stop "trying it out" and "study it first" - then learn to use it effectively.

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              B 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                :sigh: Stop doing that. Stop "trying it out" and "study it first" - then learn to use it effectively.

                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

                I did manage to come across a very good site that has a step by step guide of putting together a C# program. This should show me the whole picture of how things work together. [DELETED] Brian

                OriginalGriffO 1 Reply Last reply
                0
                • B Brian_TheLion

                  I did manage to come across a very good site that has a step by step guide of putting together a C# program. This should show me the whole picture of how things work together. [DELETED] Brian

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #28

                  Your other message with the same site came up in moderation - rightly - and has been closed as spam. I edited this one to remove the very similar link. PLease don't post links to "tutorial sites" again, as you will get flagged as a spammer and thrown off the site. I haven't started the process this time as I think it's innocent, or at least probably innocent, but if you post more then someone will. We don't like spammers, or people who look like spammers - understandable as this site it entirely paid for by advertising and letting people advertise for free would kill that revenue stream and the siet would close. So when you post a message like "I found this wonderful site ... url ..." you are going to be closed down pretty rapidly. Be told!

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  B 2 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Your other message with the same site came up in moderation - rightly - and has been closed as spam. I edited this one to remove the very similar link. PLease don't post links to "tutorial sites" again, as you will get flagged as a spammer and thrown off the site. I haven't started the process this time as I think it's innocent, or at least probably innocent, but if you post more then someone will. We don't like spammers, or people who look like spammers - understandable as this site it entirely paid for by advertising and letting people advertise for free would kill that revenue stream and the siet would close. So when you post a message like "I found this wonderful site ... url ..." you are going to be closed down pretty rapidly. Be told!

                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

                    Looks like I should have read the rules to this site before posting. I'm used to seeing links in other sites. Thanks for pointing this out to me. Brian

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Your other message with the same site came up in moderation - rightly - and has been closed as spam. I edited this one to remove the very similar link. PLease don't post links to "tutorial sites" again, as you will get flagged as a spammer and thrown off the site. I haven't started the process this time as I think it's innocent, or at least probably innocent, but if you post more then someone will. We don't like spammers, or people who look like spammers - understandable as this site it entirely paid for by advertising and letting people advertise for free would kill that revenue stream and the siet would close. So when you post a message like "I found this wonderful site ... url ..." you are going to be closed down pretty rapidly. Be told!

                      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

                      One thing I don't understand Griff, I was given a link on this site to download a free book on C# (DotNetBookZero), how is that different from what I did by providing a link to a free pdf file that can be downloaded. Also some on this site have given me direct links to books I can buy on C#. Brian

                      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