No way to anchor qualified name?

M

Michi

There appears to be no way to anchor a qualifed name at
the global scope. Consider:

namespace foo
{
class System
{
public static void op() {}
}

class bar
{
bar()
{
foo.System.op(); // Fine
System.Console.Writeline("Hello"); // Error
}
}
}

The compiler complains that foo.System does not contain a
definition for Console. Looking through the doc, I cannot
find a way to anchor a qualified name at the global scope
and, due to name hiding, in the scope of bar(), "System"
always refers to foo.System instead of the global System
namespace.

I'm looking for a generic solution to this problem,
rather than a specific one: the problem arises in the
context of a CORBA-like system where an interface
definition language is mapped to various implementation
languages. To the interface definition language, "System"
has no special meaning, yet the generated code must avoid
name clashes of this kind.

Thanks,

Michi.
 
M

Mattias Sjögren

Michi,
The compiler complains that foo.System does not contain a
definition for Console. Looking through the doc, I cannot
find a way to anchor a qualified name at the global scope
and, due to name hiding, in the scope of bar(), "System"
always refers to foo.System instead of the global System
namespace.

You could add

using MySystem = System;

at the top of the source file, and then write

MySystem.Console.WriteLine


There will be a better solution in v2.



Mattias
 
M

Michi

-----Original Message-----
You could add

using MySystem = System;

at the top of the source file, and then write

MySystem.Console.WriteLine


There will be a better solution in v2.

Thanks for that, Mattias, this will get me off the hook.
Ideally, I'd like to be able to
write ::System.Console.WriteLine (or
even .System.Consol.WriteLine) to anchor the name lookup,
but the workaround you suggest will do for now, thanks!

Cheers,

Michi.
 
D

Daniel O'Connell

Michi said:
Thanks for that, Mattias, this will get me off the hook.
Ideally, I'd like to be able to
write ::System.Console.WriteLine (or
even .System.Consol.WriteLine) to anchor the name lookup,
but the workaround you suggest will do for now, thanks!

For clarity, Mattias(can't find that link you directed me to last time),
isn't that pretty close to the syntax v2 offers? ::System etc?
 
M

Mattias Sjögren

For clarity, Mattias(can't find that link you directed me to last time),
isn't that pretty close to the syntax v2 offers? ::System etc?

I believe the current syntax, that was presented at the PDC (unless of
course it has changed again since) is

global::System.Whatever

See Hejlsberg's PDC slides on MSDN for more details.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top