10

If I try to call my extension method which is defined like this:

Module LinqExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList
    If (source Is Nothing) Then
        Throw New ArgumentNullException("source")
    End If
    Return New SortableBindingList(Of TSource)(New List(Of TSource)(source))
End Function
End Module

by calling

   Dim sss As String()
   sss.AsEnumerable.ToSortableBindingList()

it gives an error "ToSortableBindingList is not a member of System.Collections.Generic.IEnumerable(Of String)".

Note: Intellisense autocompletes after the last period! If I try to call context.TestTable.AsEnumerable.ToSortableBindingList (TestTable is a pure EF4 generated class) it not even shows up with intellisense. I don't get why. What's wrong with the extension method declaration "ByVal source As IEnumerable(Of TSource)"?

*********************************** EDIT ********************************

Ok, to clarify what's happening I'd like to provide some additional info. I can track down the problem to the following:

Scenario:

Assembly1 (root namespace "myapp"):

...
     <System.Runtime.CompilerServices.Extension()> _
        Public Function ToTest(ByVal source As String) As String
            Return ""
        End Function
...

'Calling works:

...
Dim a as string
a.ToTest()
...

Assembly2: (Reference to Assembly1 included)

'Calling does not work:

imports myapp
...
Dim a as string
a.ToTest()
user449253
  • 135
  • 1
  • 8
  • Note that it seems to be a problem with my namespace. The extension method resides in a different project with a different namespace which I import as usual (I can see all other methods with intellisense!) – user449253 Sep 16 '10 at 08:34
  • Yes, for extension methods to work, they must be in the scope - that's how compiler (and intellisense) search for them. I will advise you to add your comment as an answer. – VinayC Sep 16 '10 at 08:40
  • It is not the answer because I added this namespace already (therefore intellisense is working but nevertheless it underlines the call and is giving the error I already reported) – user449253 Sep 16 '10 at 08:42

1 Answers1

18

Your namespace "myapp" cannot directly contain the function "ToTest", there is a Module defined there. Try

Imports myapp.LinqExtensions

and make sure it is a Public Module

Willem
  • 5,364
  • 2
  • 23
  • 44
  • I can't import the module (an error is showing up in the IDE). I think this is because modules implicitly import their names into the current namespace. – user449253 Sep 17 '10 at 07:09
  • 6
    did you define the module as a Public Module? If not it would not be accessible from another assembly. – Willem Sep 17 '10 at 08:43
  • 1
    You're absolutely right. If look closely enough I see that "public" is indeed missing... now it's working. Thank you very much! – user449253 Sep 17 '10 at 11:44