Dean Kuga
Dean Kuga

Reputation: 12131

Why is this extension method not working?

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...

Upvotes: 7

Views: 14803

Answers (6)

scil
scil

Reputation: 307

I had this same issue. My mistake is the argument type is not same: defined one is Session, revokded one is ISession.

Upvotes: 0

Ali Osman Yavuz
Ali Osman Yavuz

Reputation: 417

If you are using different namespaces try this code.

namespace Extensions
{
    public static class StringExtensions
    {
        public static bool IsNumeric(this string inputString)
        {
            return decimal.TryParse(inputString, out decimal result);
        }
    }
}

namespace Business
{

    // add here other namespaces
    using Extensions;
    public static class Tools
    {
        public static bool Check(string inputString)
        {
            return inputString.IsNumeric();
        }
    }
}

Upvotes: 0

Martin Meeser
Martin Meeser

Reputation: 2956

In addition to a missing using, following case with the same symptom may occur: If you are inside a method of the class itself (or one if its implementors / inheritors), you need to make use of this.

File extension.cs:

namespace a 
{
    public static void AExt(this A a) {}
}

File user.cs

namespace a 
{

    class A {}

    class B : A 
    {
        this.AExt();
        // AExt() will not work without this.
    }
}

Upvotes: 0

Hakubex
Hakubex

Reputation: 154

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.

Upvotes: 3

p.campbell
p.campbell

Reputation: 100637

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

Upvotes: 11

gt124
gt124

Reputation: 1258

My guess is you haven't included the namespace.

Upvotes: 25

Related Questions