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. Local Variable and "global" with same name

Local Variable and "global" with same name

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
6 Posts 4 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.
  • D Offline
    D Offline
    Dwayner79
    wrote on last edited by
    #1

    I am working with two functions that format a byte array in preparation to send down the serial port (which .Net 2.0 is a but rough) and strip the formatting from the bytes recieved. These work fairly well, but in this case I had to make a few variables class wide because of the way .Net serial port works. These two functions have similar form, and as such, they use some common variable names. The one in particular was "checksum". Long story short, I move one of the function's "checksum" to a class-wide global variable and left the other in the function. I.E: namespace Coda_Verifier_Pro { public partial class MainForm : Form { byte checksum; private void myFunction1() { checksum = 0x00; //do stuff with checksum } private void myFunction2() { byte checksum; checksum = 0x00; //do stuff with checksum } } } Shouldn't the complier catch this? Am I missing something obvious? This is why I hate using Global variables. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

    S E 2 Replies Last reply
    0
    • D Dwayner79

      I am working with two functions that format a byte array in preparation to send down the serial port (which .Net 2.0 is a but rough) and strip the formatting from the bytes recieved. These work fairly well, but in this case I had to make a few variables class wide because of the way .Net serial port works. These two functions have similar form, and as such, they use some common variable names. The one in particular was "checksum". Long story short, I move one of the function's "checksum" to a class-wide global variable and left the other in the function. I.E: namespace Coda_Verifier_Pro { public partial class MainForm : Form { byte checksum; private void myFunction1() { checksum = 0x00; //do stuff with checksum } private void myFunction2() { byte checksum; checksum = 0x00; //do stuff with checksum } } } Shouldn't the complier catch this? Am I missing something obvious? This is why I hate using Global variables. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      What is it that you were expecting the compiler to catch? In myFunction1 you are accessing the global checksum variable. In myFunction2, you are modifying a local variable (local to the function). The fact that they have the same name is irrelevant to the compiler as it is able to figure out the correct scope for each variable access. If you want to be clearer about it, you could use this.checksum in myFunction1 which explicitly states you are using the global variable.

      ----------------------------- In just two days, tomorrow will be yesterday.

      D 1 Reply Last reply
      0
      • S Scott Dorman

        What is it that you were expecting the compiler to catch? In myFunction1 you are accessing the global checksum variable. In myFunction2, you are modifying a local variable (local to the function). The fact that they have the same name is irrelevant to the compiler as it is able to figure out the correct scope for each variable access. If you want to be clearer about it, you could use this.checksum in myFunction1 which explicitly states you are using the global variable.

        ----------------------------- In just two days, tomorrow will be yesterday.

        D Offline
        D Offline
        Dwayner79
        wrote on last edited by
        #3

        I thought in VS2003, it would compile error (or at least warn) of variables in the same namespace that have the same name. While I see how the compiler figures it out, it does not seem like good practice??? What if I DID want to access the global checksum in myFunction2? Would this. still work?

        ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

        S G 2 Replies Last reply
        0
        • D Dwayner79

          I thought in VS2003, it would compile error (or at least warn) of variables in the same namespace that have the same name. While I see how the compiler figures it out, it does not seem like good practice??? What if I DID want to access the global checksum in myFunction2? Would this. still work?

          ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          As far as I know, the compiler has never generated such a warning for variables. As far as being good practice, I'm not sure if there are really any rules defined for this sort of issue. There are legitimate times when you want a global (class-wide) variable with a specific name and need to use a local variable (either an explicitly defined local variable or a function parameter) with the same name. It can make things difficult to keep track of which one is being manipulated, but that is generally the only issue I am aware of. In order to access the global checksum in myFunction2 you have a few choices: 1. Remove the local variable. 2. Rename the local variable. 3. Access the global variable as this.checksume

          ----------------------------- In just two days, tomorrow will be yesterday.

          1 Reply Last reply
          0
          • D Dwayner79

            I thought in VS2003, it would compile error (or at least warn) of variables in the same namespace that have the same name. While I see how the compiler figures it out, it does not seem like good practice??? What if I DID want to access the global checksum in myFunction2? Would this. still work?

            ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            I believe that the most common practices are: :: Always access class member variables using the this keyword. :: Name class member variables with a leading underscore character. I have tried both, and they work well. It's a matter of taste which one you want to use.

            --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • D Dwayner79

              I am working with two functions that format a byte array in preparation to send down the serial port (which .Net 2.0 is a but rough) and strip the formatting from the bytes recieved. These work fairly well, but in this case I had to make a few variables class wide because of the way .Net serial port works. These two functions have similar form, and as such, they use some common variable names. The one in particular was "checksum". Long story short, I move one of the function's "checksum" to a class-wide global variable and left the other in the function. I.E: namespace Coda_Verifier_Pro { public partial class MainForm : Form { byte checksum; private void myFunction1() { checksum = 0x00; //do stuff with checksum } private void myFunction2() { byte checksum; checksum = 0x00; //do stuff with checksum } } } Shouldn't the complier catch this? Am I missing something obvious? This is why I hate using Global variables. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

              E Offline
              E Offline
              ednrgc
              wrote on last edited by
              #6

              this.checksum

              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