why does vs2005 not give warning to this?
i'm using visual studio 2005 and .NET 2.0
recently i found that c# does not give any warning when you declare a class-scope variable and a function-scope variable with the same name in one class.
for example
class myclass
{
int myvar;
void myfunc()
{
int myvar = 0; //this myvar will mask the class-scope myvar in myfunc
}
}
my warning level is at 4 (max i think).
This has caused a hard-to-find bug in one my recent project where the class is large and underwent many version changes. I intended to use the class-scope myvar in myfunc() and this was fine initally when the function-scope myvar was not present. Then someday somehow i added the function-scope myvar and I ended up with taking the fresh value of the function-scope myvar everytime.
Furthermore I found this applies to properties too! A class-scope variable will mask a property of the class with the same name and the c# compiler does not give any warning!
So, do you guys agree with my finding and think that this is a potential problem? If so is there a good design pattern to avoid this pitfall? thanks
[1181 byte] By [
jjjkkk77] at [2007-11-20 10:02:17]

# 1 Re: why does vs2005 not give warning to this?
Thats because this is allowed :P
The best 'pattern' for avoiding this issue is to prepend 'this.' when you're accessing the class level variable. For example:
public class MyClass
{
private string name;
public string Name
{
get { return name; }
internal set { name = value;}
}
public MyClass(string name)
{
// Class level var is set to the value of the parameter.
this.name = name;
}
}
This means you don't have to prepend your member names with 'm_' or anything. It's a cleaner approach in my opinion, but i'd say there's easily a dozen people out there who will suggest you prepend all class level vars with 'm' or whatever so you know they're a member variable.
# 2 Re: why does vs2005 not give warning to this?
ok, so it's better to prepend 'this.' to member instance variables and classname + '.' to memeber static variables. but i think this might turn ugly once the class becomes big and makes heavy use of class members and you can't force all develops working on the class to adhere to the prepention.
# 3 Re: why does vs2005 not give warning to this?
I agree with Mutan Fruit. You are not an alchemist, you are an engineer. Write the code in the way the code has no ambiguity, it is simple to read and you always can understoo what you see.
What whit this code? What does it mean?
int a = myEstimation;
What is myEstimation? Do you think it is another temporary variable? Yes, it could. But it also could be 1) instance variable 2) static variable 3) property, both static and instance (if you are really stupid, without sorry).
So don't save you effort on wrong places. If you are writing code, don't be lazy and write the code. Maybe be you are a compiler, but you coleages aren't.
# 4 Re: why does vs2005 not give warning to this?
I agree with Mutan Fruit. You are not an alchemist, you are an engineer. --I really agree with both of them. And if you are really tooooo lazy for this.xyz ... (as I'm ) then use the method we former did in VB or C++ m_myVariable or the a bit more modern private int _myField; as a declaration of a private Fieldmember of a class.
So you also cannot error. And... There is a small hint in your intellisense but you have overseen. As during typing you should have been aware that your Field myVar is already in the intellisense so you should have been informed that it is a classmember already. So dont seek the error in MS Visual studio it wasn't their, it was your fault ;) Dont worry.Errors are there to make it better. USE 'this' Your code is much better readable.