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