views:

147

answers:

6

I can't seem to get the following extension method to be found in another class in the same namespace (MyProject.Util).

using System.Collections.Specialized;

namespace MyProject.Util
{
    public static class Extensions
    {
        public static string Get(
             this NameValueCollection me,
             string key,
             string def
        )
        {
            return me[key] ?? def;
        }
    }
}

As you can see it's basically another version of foo[bar] ?? baz, but I still don't understand why VS2008 fails to compile telling me that no version of Get takes two arguments.

Any ideas?

+3  A: 

Are you importing your namespace (with using MyProject.Util) in the file where you're using the method? The error message might not be obvious because your extension method has the same name as an existing method.

Dan Puzey
He wrote, `in the same namespace`.
SLaks
Same name is not a problem. It is working properly at my end.
Ismail
It was a typo in my code making the extensions go in a completely new namespace. Silly me.
Deniz Dogan
+3  A: 

You can't use the extension method like a static method as in NameValueCollection.Get. Try:

var nameValueCollection = new NameValueCollection();
nameValueCollection.Get( ...
tanascius
I'm not using it as a static method, that was only me trying to explain the error.
Deniz Dogan
Ok, that was the most obvious mistake in your question ...
tanascius
+1  A: 

The following seems to work for me ...

using System.Collections.Specialized;

namespace MyProject.Util
{
    class Program
    {
        static void Main(string[] args)
        {
            var nvc = new NameValueCollection();
            nvc.Get(  )
        }
    }
}


namespace MyProject.Util
{
    public static class Extensions
    {
        public static string Get(
             this NameValueCollection me,
             string key,
             string def
        )
        {
            return me[key] ?? def;
        }
    }
}

Have you checked your target framework?

WestDiscGolf
Target framework version sounds like the most likely culprit to me. @blahblah: Make sure you're targetting .NET 3.5 or later.
Damian Powell
I've always been using .NET 3.5. I'm doing this in ASP.NET MVC by the way, I'm not even sure I can do that with anything below 3.5.
Deniz Dogan
+1  A: 

Is the class in the same assembly as the class where it is being used? If no, have you added a reference to that assembly?

tvanfosson
It is in the same assembly.
Deniz Dogan
+1  A: 

Works fine when I try it. There's really only one failure mode: forgetting to add a using statement for the namespace that contains the extension method:

using System.Collections.Specialized;
using MyProject.Util;     // <== Don't forget this!
...
    var coll = new NameValueCollection();
    coll.Add("blah", "something");
    string value = coll.Get("blah", "default");
Hans Passant
A: 

I had a similar issue recently and traced it down to not referencing System.Core (the project was compiled against 3.5, but that reference had accidentally been removed while experimenting with VS2010/.Net 4.0).

Chris Shaffer
I find `System.Core` when expanding References in my VS2008 project.
Deniz Dogan