Q re Extension Methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    Q re Extension Methods

    Hi

    I have 2 classes which are both instantiated by shared factory methods
    and both implement a common interface.

    Public Class Factory

    Public Shared Function CreateMyClass1( ) As MyClass1
    Return New MyClass1
    End Function

    Public Shared Function CreateMyClass2( ) As MyClass2
    Return New MyClass2
    End Function

    End Class


    Public Interface IItemContainer
    Readonly Property ItemGroups as
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup)
    End Interface

    Public Class MyClass1
    Implements IItemContainer
    ....
    Private ReadOnly Property ItemGroups() As
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup) _
    Implements IItemGroupConta iner.ItemGroups
    Get
    Return m_itemGroups
    End Get
    End Property
    ....
    End Class

    Public Class MyClass2
    Implements IItemContainer
    ....
    Private ReadOnly Property ItemGroups() As
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup) _
    Implements IItemGroupConta iner.ItemGroups
    Get
    Return m_itemGroups
    End Get
    End Property
    ....
    End Class

    I also have an extension method so as not to duplicate code

    <System.Runtime .CompilerServic es.Extension()_
    Public Function ItemCount(ByVal p_container As IItemContainer) As Integer
    Return CType((From g In p_container.Ite mGroups _
    Select g.Value.Count). Sum(), Integer)
    End Function


    What I'm finding is that one of the classes is not recognising the
    extension methods so I'm getting compilation errors for each extension
    method call which is made. The other class is behaving correctly.

    Any thoughts

    Thx

    Simon
  • Simon Woods

    #2
    Re: Q re Extension Methods

    Oops

    I had the tests in different classes and hadn't included the necessary
    imports ...

    Sorry

    S


    Simon Woods wrote:
    Hi
    >
    I have 2 classes which are both instantiated by shared factory methods
    and both implement a common interface.
    >
    Public Class Factory
    >
    Public Shared Function CreateMyClass1( ) As MyClass1
    Return New MyClass1
    End Function
    >
    Public Shared Function CreateMyClass2( ) As MyClass2
    Return New MyClass2
    End Function
    >
    End Class
    >
    >
    Public Interface IItemContainer
    Readonly Property ItemGroups as
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup)
    End Interface
    >
    Public Class MyClass1
    Implements IItemContainer
    ...
    Private ReadOnly Property ItemGroups() As
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup) _
    Implements IItemGroupConta iner.ItemGroups
    Get
    Return m_itemGroups
    End Get
    End Property
    ...
    End Class
    >
    Public Class MyClass2
    Implements IItemContainer
    ...
    Private ReadOnly Property ItemGroups() As
    System.Collecti ons.Generic.Dic tionary(Of String, ItemGroup) _
    Implements IItemGroupConta iner.ItemGroups
    Get
    Return m_itemGroups
    End Get
    End Property
    ...
    End Class
    >
    I also have an extension method so as not to duplicate code
    >
    <System.Runtime .CompilerServic es.Extension()_
    Public Function ItemCount(ByVal p_container As IItemContainer) As Integer
    Return CType((From g In p_container.Ite mGroups _
    Select g.Value.Count). Sum(), Integer)
    End Function
    >
    >
    What I'm finding is that one of the classes is not recognising the
    extension methods so I'm getting compilation errors for each extension
    method call which is made. The other class is behaving correctly.
    >
    Any thoughts
    >
    Thx
    >
    Simon

    Comment

    Working...