2

I am trying to figure out why this is happening, but I can't. I have a WCF service and inside it I created a class, called ExtensionUtil which will consist of extension methods only.

For now the only method I have in the ExntensionUtil class is called AddWithNullableValue. It looks like this:

public static void AddWithNullableValue(this SqlParameterCollection paramCollection, string paramName, object value)
{
    SqlParameter param = new SqlParameter();

    param.ParameterName = paramName;
    param.Value = value == null ? DBNull.Value : value;

    paramCollection.Add(param);
}

I am using this method in the DAL like this:

sqlCommand.Parameters.AddWithNullableValue("@categoryId", publication.CategoryId);

Everything compiles fine. However, when I try to start the service a compilation error occurs, stating:

The call is ambiguous between the following methods or properties:
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)' and
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)'

Why is this happening? I have only one method with this name and the compiler says that the call is ambiguous.

Edit:

When I remove the 'this' keyword in front of the SqlParameterCollection paramater, making the method an ordinary static method, instead of an extension one, everything works okay. Moreover, I looked in the entire solution (using Ctrl+Shift+F) for all occurraces of the 'AddWithNullableValues' keyword and the search resulted in the following: the signature of the method and the two places in my code where I call it. I then even created a completely new static class, added the same method with a completely different name and the same body and I still got the same error when I called it, so the problem must be something else.

Finally, I needed another extension method and when I called it in my code I got the same error for it.

public static Nullable<T> GetNullableValue<T>(this SqlDataReader sqlDataReader, string columnName) where T : struct
{
    int columnIndex = sqlDataReader.GetOrdinal(columnName);

    Nullable<T> value = null;

    if (!sqlDataReader.IsDBNull(columnIndex))
    {
        value = (Nullable<T>)sqlDataReader.GetValue(columnIndex);
    }

    return value;
}

It seems to me that the solution has some problem with the extension methods and I don't know why. I built it successfully a couple of times, tried to clean it - everything works okay, until I try to use an extension method as an extension method, not as a static method.

Yulian
  • 6,262
  • 10
  • 65
  • 92
  • 1
    "...there's only a single method with the given name..." - You sure about that? Can we see your namespace declaration? The compiler seems to think you have two; one in `ExtensionUtil` and one in `ExtensionMethod`. Did you rename one, move things around, etc.? – Ed S. Jun 09 '14 at 23:31
  • I've moved your error message around a bit so that you can see the two declarations side by side. Notice that one of your declarations is in the `App_Code.Util.ExtensionUtil` namespace, and the other is in the `App_Code.Util.ExtensionMethod` namespace. – Robert Harvey Jun 09 '14 at 23:31
  • Try to clean your project and recompile. – MAV Jun 09 '14 at 23:31
  • I copied the source of the method, deleted the class and created a new one with the same name. Everything built successfully again, and when I started the service the same error occurred again. – Yulian Jun 09 '14 at 23:39
  • Now I got even more confused. I removed the 'this' keyword in front of the SqlParameterCollection paramater, making the method an ordinary static method, instead of an extension one. Now everything works okay. – Yulian Jun 09 '14 at 23:46
  • 1
    That's because you changed the signature of the duplicate method. Use your tools, man. Do a search for the method, and find the dupe. – Robert Harvey Jun 10 '14 at 03:30
  • I looked in the entire solution (using Ctrl+Shift+F) for all occurraces of the 'AddWithNullableValues' keyword and the search resulted in three matches - the signature of the method and the two places in my code where I call the method. Something strange is happening here (: – Yulian Jun 10 '14 at 19:51
  • I even created a completely new static class, added the same method witha different name and the same body and I still get the same error when I call it, so the problem must be something else. Can anybody copy the code and try to run it in his own project, please? – Yulian Jun 10 '14 at 19:59
  • I found the solution to the problem, check the best answer :) – Yulian Jun 12 '14 at 05:57

3 Answers3

5

Something similar happened to me when Visual Studio referenced a project to itself (no clue why). I was able to solve it be simply removing the nonsensical reference.

jahu
  • 5,427
  • 3
  • 37
  • 64
3

I found the solution to this problem and it worked for me. It's quite disturbing but you can read the complete answer here.

App_Code is not supported in a WAP. The App_Code folder is compiled at run-time; all code in a WAP is compiled at compile / development time. So, when you add an App_Code folder to a WAP. you end up with duplicate code; for example, a class defined in App_Code will also show up in the wap DLL. The fix is easy: just name the folder something else like Classes or CodeFiles.

By the way, thanks to all of you who tried to help me :)

Community
  • 1
  • 1
Yulian
  • 6,262
  • 10
  • 65
  • 92
0

The error message shows you have 2 methods with the same name and signature.

You need to either call it from the specific class or give them different namespaces. The caller is confused on which extension method you are trying to use and I believe the next version of C# will allow you to specify a namespace when calling an extension method.

If one of those does not actually exist anymore, then make sure you clean and build again to try and get rid of any leftovers.

TyCobb
  • 8,909
  • 1
  • 33
  • 53