Last post May 24, 2013 02:15 AM by CasperGui
Jul 12, 2011 05:42 PM|merkava|LINK
I have an extension method for an object. When I call this method from another extension method, I get an error from the compiler saying the call is ambiguous. Here is a simplified version of my code:
public static class MyExtension
public static string Method1(this string str, string someString)
public static string Method2(this string str, string someString)
string someString2 = str.Method1(someString); //<== compiler does not like this
Compiler Error Message: CS0121: The call is ambiguous between the following methods or properties: 'MyExtension.Method1(string,
string)' and 'MyExtension.Method1(string, string)'
What am I doing wrong here?
Thanks in advance for your help.
Jul 12, 2011 06:02 PM|kedarrkulkarni|LINK
check ambiguity section in below link
hope this helps...
Jul 12, 2011 06:04 PM|LudovicoVan|LINK
That code compiles just fine...
Jul 13, 2011 05:47 PM|merkava|LINK
Thanks for your answer.
my case is different from the examples of the link.
My method is never declared twice in different classes. It is calling the method from another method that makes the compiler unhappy.
Jul 13, 2011 05:55 PM|merkava|LINK
some clarification: yes the site will build whit success. but when running, I get the compiler error described above.
Also did you set the buid action of the class to compile? by default VS set it to content.
thank you for your answer.
Jul 14, 2011 07:12 PM|LudovicoVan|LINK
When it runs you get runtime errors, if you get compiler errors that is when it does not buld. And by default the action is compile.
So, all in all, what you are saying is simply nonsense, plus unless you provide a code that does fail there is no way to help.
I would usually say that you are simply trolling.
[MOD] Of course this question has nothing to do with architecture: I'd suggest to move it to C#.
Jul 15, 2011 12:01 PM|merkava|LINK
Forgive my ignorance O Master! yes, you are right, it is a runtime error and not a compiler error (althought the error message says "Compiler
was polite in my response and thanked you. If you cannot help, spare me your condescendence.
The code I provided does fail.
Any help will be greatly appreciated.
Jul 15, 2011 02:53 PM|LudovicoVan|LINK
I have not been "unpolite", there are no personal appreciations in my message, you might at best say that I am somewhat rude, but even that is relative to one's personal and technical culture. That what you are saying makes no sense is a matter of fact.
As to your problem, all I can suggest is that you do a find across files to see if you have some other Method1 defined somewhere: that is all I can suggest, given the few lines of pseudo-code you have provided (which do compile just fine), and the total
lack of information about the specific scenario in which that is happening. The rest is guessing, which is something that just does not belong to my competences.
Jul 15, 2011 04:17 PM|merkava|LINK
LV, let's not pollute this great forum with useless arguments.
As per your suggestion, I posted my problem in a section more appropriate than "architecture" and with the help of other members of this forum, I found the solution. The correct code is:
string someString2 = Method1(str, someString);
When within my extension class, I have to pass the base object as parameter. Somehow it make sense: I cannot use the extension within its definition.
Thank you for help and for taking the time to look at my issue.
Jul 15, 2011 04:54 PM|LudovicoVan|LINK
> I cannot use the extension within its definition
Of course you can: the code below compiles just fine too (although, as it is, it of course gets into a non-terminating recursion).
Anyway, given that you have marked your post as an answer, you could have at least posted a link to the explanation and supposed solution...
public static class StringExtensions
public static string MyMethod1(this string input, string s)
Jul 15, 2011 06:47 PM|merkava|LINK
all right, got the tada! moment. Happened to be a completely different problem: it was not my code that was wrong but where I had put it.
I am in a Web Application Project. In a WAP, the code can be put anywhere, right? Well, not anywhere. To make it consistent with other app folder names (App_Themes, App_Data), I put the code in the App_Code folder. Wrong idea. Even in a WAP, the App_Code
folder is a special folder and the code inside get compiled a second time at runtime.
Now that I have renamed the folder AppCode, all is good and the sun is shining. My initial code compile and run just fine (so does yours). Bonus, when I create a new class in the renamed folder, it get the build action set to Compile by default.
I am not sure now I understand why what I though was the fix actually did work.
In fact, I don't understand why I didn't run into trouble before.
Jul 15, 2011 08:32 PM|LudovicoVan|LINK
Just checked in .Net 4 and everything does work, even in an App_Code folder, even when the action is left to Content, although some of the compiler settings work better if the action is set to Compile. Maybe it didn't work in previous versions? I just
won't bother to check that, now this is enough.
But glad we have got nearer, plus now you get the chance for a third problem and solution...
May 24, 2013 02:15 AM|CasperGui|LINK
I had the same problem. The mistake I had was that I had written:
public static class CommandExtensions
I changed it to:
static class CommandExtensions
and it worked for me....