Hi All,
In my solution I have 2 classes with the same name in the same namespace.
This was legal in VS.net 2003 (.Net 1.1) and all the compiler did was to raise warning about this problem. However with VS.net 2005 (.Net 2.0), this has changed it now throws a compiler error for the same. Is it possible to get around this issue in any way without having to change the class names...
Thanks in Advance
andypai

Classes with the same name in the same namespace....
Andymcdba1
James, I agree with you and if only I coul find the developers who wrote that code......
Any way coming back to the main point, I guess the only elegant way out would be to rename those classes and fix all the lines of code where this is referenced..
Thanks for your help.
Regards
Andy
BortNE24
I just did a small test in .NET 1.1 but even their it is not allowed.
Can you sent me the code that you used in .NET 1.1 to see what you mean exactly
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
My Personal Blog
Johnny Funch
I don't think that this is possible. But why do you want to have multiple classes with the same name in one namespace Anyway, it is not a good practice to do this, even if it was possible in .NET 1.1 (never tried).
For example, what would happen if you want to do a cast from an object to your class like this:
MyClass x = (MyClass) obj1;
The compiler can not decide which of the types he has to use.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
My Personal Blog
rob_a89
spree
I think using partial will end up merging them into one huge class. Maybe you can wrap them each with a new class that have different names. So they become nested inside different class names.
Will Merydith
Sorry, but I'm a little confused about the outcome of this thread (and this reply is in no way meant to offend James).
Fixing this problem is as simple as adding the "Partial" keyword to each class.
Does this work with classes in seperate assemblies Yes.
Does this merge all classes into one big assembly Only if the classes are in the same project. if they are in seperate projects they can be in seperate assemblies.
Is this an undocumented feature No. http://msdn2.microsoft.com/en-us/library/wa80x488.aspx
Is this bad coding Most definately not. Typed Datasets use this feature to seperate designer code from code added by the developer.
Should developers who implement this feature be sacked Most definately not
Therefore, could some please explain how the James' reply answered this thread
kkt
Ahh, I understand where you're coming from and agree that if the two classes are not related they should have different names. I was obviously looking at things from a different perspective and assumed that the two classes were in fact related.
Agree partial is intended to be used to split classes into more than one source file. This is perfect for auto-generated classes as it stops custom code from being deleted when the class is regenerated.
sveroa
So, the functioning of your appliation depends upon an undocumented compiler behavior, which may well change on the next release of the compiler. Oh, wait.... It depended upon an undocumented compiler behavior which DID change on the last release of the compiler.
It was always broken.
It always needed to be changed.
Find the programmer who thought that was a good idea, and slap him (or at least suggest a different career)
Then more one of the classes into a different namespace.
Adair
arifyemen
I was not talking about the "fix" of using partial. I was talking about the original situation where two different classes had the same name.
Moving on, using "partial" is not a solution in this case. It would merely be hiding the problem.
"partial" is intended for the cases when one class needs to be divided over more than one source file. That is not the case here, where we have two completely different classes, which just happen to have the same name. Merging these two distinct classes into one big one may mask the immediate problem, but is likely to cause bigger problems later. (ie, there's a good chance that they have common methods, which would conflict when merged).
They are two distinct classes; they deserve distinct names. The easiest way would be to put one in a different namespace.
Jon Stroh
Hi Geert,
It's possible if the classes are in separate assemblies. I have added a code sample below...
e.g
In Assembly1:
---------------------
using System;
namespace MyNameSpace
{
/// <summary>
/// Summary description for SameNameClass.
/// </summary>
public class SameNameClass
{
public SameNameClass()
{
//
}
}
}
---------------------
In Assembly2:
---------------------
using System;
namespace MyNameSpace
{
/// <summary>
/// Summary description for SameNameClass.
/// </summary>
public class SameNameClass
{
public SameNameClass()
{
//
}
}
}
---------------------
Assembly3: References both Assm1 and Assm2
---------------------
using System;
namespace MyNameSpace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
SameNameClass samename = new SameNameClass();
}
}
}
---------------------
This code compiles. The compiler picks up the defintion from the first reference in its refernce list that matches...
I agree it is bad coding practice but I have inherited this piece of code from the previous team and I dont want to make changes unless I really have to :-)
Cheers
Andy
liujj_xujj