C# Collision
August 12, 2006 8:48 PM   Subscribe

C#. I have a static method in class Foo that needs to call a static method called Qux in class Bar. Problem is, there's a static method in Foo called Bar.



Surely there's some piece of syntax that will let me call Bar.Qux?
posted by iconjack to Computers & Internet (16 answers total)
 
Place the Bar class into its own namespace — separate from namespace in which Foo resides — e.g. MyBar, and refer to the full path MyBar.Bar.Qux() inside your Baz() method, instead of just Bar.Qux().
posted by Blazecock Pileon at 8:56 PM on August 12, 2006


what about adding a namespace around the whole thing:

namespace Doom
{
class Foo
{
static void Baz()
{
Doom.Bar.Qux();
}
static void Bar() {}
}
class Bar
{
public static void Qux() {}
}
}
posted by clarahamster at 9:06 PM on August 12, 2006


oops, sorry about the formatting. looks like metafilter ate my spacing. :)
posted by clarahamster at 9:06 PM on August 12, 2006


Response by poster: clarahamster: I already have a namespace around the whole thing. Doesn't seem to matter.

blazecock: Unfortunately, I don't have the freedom in this codebase to change any namespaces.

re:formatting: Now you know why I inserted the code as an image!
posted by iconjack at 9:35 PM on August 12, 2006


I don't think you can solve this without namespaces. In fact, what you're doing is not good coding style, without using them — this is the very problem namespaces solve.
posted by Blazecock Pileon at 9:41 PM on August 12, 2006


Response by poster: OK, fair enough. Unfortunately I don't have control of the namespaces.

And I still don't see why the compiler is confused.
The line Bar.Qux() is obviously a call to method Qux of class Bar. So what if Bar is also the name of a method.
posted by iconjack at 9:48 PM on August 12, 2006


Best answer: You could subclass Bar. Then add one static method: an alias to Bar.Qux() that's not called Qux().

A bit of a hack, admittedly. But from what I can tell, you seem to be in a bind.
posted by maschnitz at 9:48 PM on August 12, 2006


Best answer: There's a namespace alias feature that lets you handle this:

using BarClass = WhateverNamespace.Bar;
[..]
static void Baz()
{
BarClass.Qux();
}
posted by inkyz at 10:43 PM on August 12, 2006


(I don't know C#, and I understand it's more like Java lthan C++, but....) In C++, you'd use teh scope resolution operator, like so:
Bar::Qux() ;

Inky's suggestion seems best, however.


iconjack writes "blazecock: Unfortunately, I don't have the freedom in this codebase to change any namespaces."

Again, admittedly using C++ rules, namespaces van be opened whether or not it's your code that declared the namespace

iconjack writes "clarahamster: I already have a namespace around the whole thing. Doesn't seem to matter."

Ok, but are you fully qulaifying the name as clarahamster suggests?
posted by orthogonality at 10:57 AM on August 13, 2006


Response by poster: Ok, but are you fully qulaifying the name as clarahamster suggests?

All the code is under one namespace, so adding the namespace qualifier in front doesn't help.
posted by iconjack at 12:41 PM on August 13, 2006


All the code is under one namespace, so adding the namespace qualifier in front doesn't help.

No, orthogonality is correct. If you have

namespace foo {
class A { public int B(); }
class B {}
}

then foo.B always means the class B, not the method B. Once you're using the namespace you're talking full names, so to refer to the method you'd have to do foo.A.B
posted by inkyz at 1:21 PM on August 13, 2006



In C# 2.0 you can prefix your name with 'global::' to disambiguate between the function Bar (which is part of the class) and the class Bar (which is global.)

Caveat: I know nothing about C#, I just did a google search for "C# global namespace prefix", and found this page, item 7.1.


C++ has a similar feature (just use the "::" prefix,) but, as mentioned earlier "Bar::Quux()" seems to 'just work' in C++.
posted by blenderfish at 2:06 PM on August 13, 2006


(Very parenthetical: this is why having a public standards document is a good thing. C++ has one, the proprietary language C#, as far as I know, doesn't. Since the C++ Standard requires conforming compilers to implement certain namespace features, I know that either Bar::Qux() calls the Qux member of Bar (if I'm using the namespace containing Bar), or it's ambiguous (if, for example, there's a namespace Bar with a Qux declared in it), or the compiler is wrong. With C#, I don't have a public Standard, so I don't have the satisfaction of knowing what has to work.)
posted by orthogonality at 4:09 PM on August 13, 2006


Although you've already checked the 'using' syntax as the correct answer, I'd claim that to get this conflict you've got some poorly named methods and classes and that the correct way to fix it is to refactor into better class method names or a more appropriate division of namespaces.

If your question is just the generic "is there a purely syntactic construct avoid this naming conflict?", well, there's your answer.
posted by plinth at 6:36 PM on August 13, 2006



Very parenthetical: this is why having a public standards document is a good thing. C++ has one, the proprietary language C#, as far as I know, doesn't.

Actually, Microsoft's been very proactive about getting C# approved as a standard. I don't know all the ins and outs of standardization, but there's definitely an ECMA standard document for C#.

posted by inkyz at 8:18 PM on August 13, 2006


inkyz writes "Actually, Microsoft's been very proactive about getting C# approved as a standard. I don't know all the ins and outs of standardization, but there's definitely an ECMA standard document for C#."


No shit, I'm wrong! Ok, since there is a Standard, in penance I will learn C#.
posted by orthogonality at 10:09 AM on August 14, 2006


« Older Dangerously teetering on shut-in   |   Can a consignment shop get away with selling fake... Newer »
This thread is closed to new comments.