473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class and namespace with same name

Easier to show than to explain:

file 1:
---------------------------------------------------
namespace Somename {
public class Somename {
....
}
}

file 2:
--------------------------------------------------
using Somename;
......

Somename s1 = new Somename; // Error!
Somename s2 = new Somename.Somename; // OK
--------------------------------------------------

This generates an error because the compiler thinks it is ambiguous.
It relates to the fact that the namespace and class in file 1 have the
same name. Changing either name fixes it.

Not sure why. You'd think it would be qualified by the 'using'
statement. Is this according to spec? I thought I had seen this done
without the error message.

Nov 15 '05 #1
2 11939
n!
> Not sure why. You'd think it would be qualified by the 'using'
statement. Is this according to spec? I thought I had seen this done
without the error message.


This is down to the way the language determines what your declaration is
referring to. When looking up a typename, the language checks locally before
trying other areas of the code. As the namespace 'Somename' is within the
namespace you're working in (I guess you're at global scope, in the example)
it finds that first. If you are within an unrelated namespace, it should
fail to find 'Somename' and then proceed to look inside the 'Somename'
namespace (because of your 'using' statement). It will also work if your
code referencing the 'Somename' class is within the 'Somename' namespace, as
it will find the class first in its search :)

Hmmm, I think I made that as clear as mud, sorry.

n!
Nov 15 '05 #2
_ed
On Tue, 27 Jan 2004 10:00:04 -0000, "n!" <nf********@nomailplease.com>
wrote:
Not sure why. You'd think it would be qualified by the 'using'
statement. Is this according to spec? I thought I had seen this done
without the error message.


This is down to the way the language determines what your declaration is
referring to. When looking up a typename, the language checks locally before
trying other areas of the code. As the namespace 'Somename' is within the
namespace you're working in (I guess you're at global scope, in the example)
it finds that first. If you are within an unrelated namespace, it should
fail to find 'Somename' and then proceed to look inside the 'Somename'
namespace (because of your 'using' statement). It will also work if your
code referencing the 'Somename' class is within the 'Somename' namespace, as
it will find the class first in its search :)

Hmmm, I think I made that as clear as mud, sorry.


Ha! About as clear as my illustration permitted, I guess. Yes, I
should have clarified that the caller is inside another namespace.

Revised:

file 1:
---------------------------------------------------
namespace Somename {
public class Somename {
....
}
}

file 2:
--------------------------------------------------
using Somename;

namespace Anothername {
class Anotherclass {
public static void Main() {
Somename s1 = new Somename; // Error!
Somename s2 = new Somename.Somename; // OK
}
}
}
--------------------------------------------------

Yes, I do see what's going on; the caller is looking for namespaces
first. It finds one, then (even though that namespace was already
called out in the 'using' statement) it assumes that it is looking at
a namespace ref and not a class ref. It looks no further.

I thought that I had seen code that implied the compiler handled this
more 'intelligently'. I guess not. For the number of silly things
that they *did* do (overloading 'new' and 'using' require analysis of
context), I thought they could handle it.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matt Mastracci | last post by:
It seems like the C# compile can't resolve the difference between a namespace and a class in a situation that is clearly unambiguous. Note that it sees the namespace "C" before the class "C". I...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
5
by: Xarky | last post by:
Hi, I have the following in the project, with the following desgin. // class 1, which is a windows form namespace Name { public class aaaa {
13
by: Duron | last post by:
I created a new folder using VS.NET 2003. Then I created a new web form under that folder, say, \Member\Default.aspx. However, even if I didn't do anything to that page, a run-time error appears...
2
by: Shapper | last post by:
Hello, In the main root of my web site together with my aspx and aspx.vb files I have the file global.vb. This file has a class with all the functions which are used in many aspx.vb files and...
13
by: Brian | last post by:
I have many similar classes in a project, one for each type of report my app can create. I want to instantiate them based on a value passed in by a scheduler module. Right now I have Sub...
0
by: Edmund Green | last post by:
Included below is a minimal web-service implementation to recreate a problem I've encountered in a web service that has methods returning classes with the same local name (but in different...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
7
by: Juha Nieminen | last post by:
This is possible: namespace X { class A; } class X::A { <implementation}; However, what about nameless namespaces? Does this do what I want? namespace { class A; }
8
by: Jack | last post by:
Hi, I have a single class XXX residing in a namspace with the same name: XXX. I can access my class in my code just fine with: XXX.XXX.MySub() '(Note: shared sub!) But I don't want to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.