• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

type and var name with same name

Red Squirrel

No Lifer
Lets say I have a class, and in that class I declare a variable of type Guild and call it Guild, then in a function of that class I want to declare another variable of type Guild so I go:


Guild newguild;


How do I tell the compiler that by Guild I'm refering tothe type, and not the name of the variable within the class?

Whoever coded the original to what I'm working on used some very bad naming convention and should never haved used the same name as the type. Right now the only work around I can think of is renaming it, though this will require to edit a couple 100 files in the program, so I'm trying to avoid this unless its really the only way.
 
Wow, what language is it? I know Java has no problem with this (although the variable name does go against Sun's coding conventions):
public class VarAndClassSame {
public static void main(String[] args) {
VarAndClassSame VarAndClassSame = new VarAndClassSame();
System.out.println(VarAndClassSame.hashCode());
System.out.println(VarAndClassSame.class.getSimpleName());
}
}

Compiler just figures it out from the syntax. You are never going to start a variable declaration with a variable name instead of a class name, for example.
 
Oh my bad I could have sworn I said it was C# but guess I did not.

Anyway I figured it out, when I refer to the type I have to specify the full namespace name as well then it knows.
 
Originally posted by: RedSquirrel
Oh my bad I could have sworn I said it was C# but guess I did not.

Anyway I figured it out, when I refer to the type I have to specify the full namespace name as well then it knows.

Shouldn't u be able to refactor the variable name to guild or something less confusing? I know in Java-Eclipse you can just hit ctrl+shift+r and then it will rename that variable and all references to it correctly -- i.e. not just a plain find/replace. I'm sure Visual C# must allow this... a quick google shows this:

http://www.devsource.com/article2/0,1895,1828694,00.asp

 
Yeah it was badly coded from the start - not my doing, at worse case I would of went and changed all references, but there is at least 2,500 files to this app. I do have a program that can do this but it could cause issues if the word guild shows up in other contexts. This situation was a rare occurance so the namespace method works fine.
 
If you're using C#, Visual Studio's refactor functionality should work at least as well as an external program...

Though it's not always the most adept at deciphering conflicting naming schemes.
 
Back
Top