Why is this extension method not working?

11,257

Solution 1

My guess is you haven't included the namespace.

Solution 2

Ensure this method is in a static class of its own, separate class from the consuming DataRow.

namespace MyProject.Extensions
{
   public static class DataRowExtensions
   {
      //your extension methods
   }
}

In your consumer, ensure you're:

using MyProject.Extensions

Solution 3

I had this same issue. My mistake wasn't that I missed the static class or static method but that the class my extensions were on was not public.

Share:
11,257
Dean Kuga
Author by

Dean Kuga

Full stack .NET developer. C# WPF LINQ Entity Framework Winforms ASP.NET/MVC WCF WebAPI

Updated on June 05, 2022

Comments

  • Dean Kuga
    Dean Kuga almost 2 years
    public static string ToTrimmedString(this DataRow row, string columnName)
    {
        return row[columnName].ToString().Trim();
    }
    

    Compiles fine but it doesn't show up in intellisense of the DataRow...

    • Sanjeevakumar Hiremath
      Sanjeevakumar Hiremath about 13 years
      shows in intellisense in my VS10. just tested it.
    • Mike Cole
      Mike Cole about 13 years
      Please post the incomplete line where you are trying to call the extension method.
    • marc_s
      marc_s about 13 years
      Also: be careful, if the row doesn't contain any column by the name of columnName you'll end up getting an exception - you might want to check for that and return null or an empty string if that's the case
    • Dean Kuga
      Dean Kuga about 13 years
      @marc_s Absolutely, this was just for simplicity...
    • Mustafa Bazghandi
      Mustafa Bazghandi about 2 years
      Extension methods should be define inside a static class.
  • Brian
    Brian about 13 years
    I don't think it would compile if it weren't in a static class
  • Dean Kuga
    Dean Kuga about 13 years
    using System.Data; is included... Do you think it would compile if using System.Data; was not included?
  • gt124
    gt124 about 13 years
    The namespace of the class that has the extension method.
  • Dean Kuga
    Dean Kuga about 13 years
    that could be it, the class from which I'm trying to use the method is a "sub" namespace of the namespace the extension method is in... will accept if that turns out to be the issue... thanks for clarification...