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.
Author by
Dean Kuga
Full stack .NET developer. C# WPF LINQ Entity Framework Winforms ASP.NET/MVC WCF WebAPI
Updated on June 05, 2022Comments
-
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 about 13 yearsshows in intellisense in my VS10. just tested it.
-
Mike Cole about 13 yearsPlease post the incomplete line where you are trying to call the extension method.
-
marc_s about 13 yearsAlso: be careful, if the
row
doesn't contain any column by the name ofcolumnName
you'll end up getting an exception - you might want to check for that and returnnull
or an empty string if that's the case -
Dean Kuga about 13 years@marc_s Absolutely, this was just for simplicity...
-
Mustafa Bazghandi about 2 yearsExtension methods should be define inside a static class.
-
-
Brian about 13 yearsI don't think it would compile if it weren't in a static class
-
Dean Kuga about 13 years
using System.Data;
is included... Do you think it would compile ifusing System.Data;
was not included? -
gt124 about 13 yearsThe namespace of the class that has the extension method.
-
Dean Kuga about 13 yearsthat 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...