扩展方法不是List(Of ...)的成员

时间:2019-07-19 19:45:39

标签: vb.net extension-methods

我已经在我的项目中添加了以下内容:

Imports System.Runtime.CompilerServices

Module ExtensionMethods

    <Extension()>
    Public Sub Move(Of T)(ByVal uBase As List(Of T), ByVal index As Integer, ByVal newIndex As Integer)

        If uBase.Count <= newIndex Then
            Debug.Assert(False)
            Return 'not possible!
        ElseIf newIndex < 0 Then
            Debug.Assert(False)
            Return 'not possible!
        End If

        Dim item As T = uBase.Item(index)
        uBase.RemoveAt(index)
        uBase.Insert(newIndex, item)

    End Sub

End Module

然后我创建了一个课程:

Public Class clsPageWordList

    Private m As New List(Of udtWordImage)

    Private m_CountCellsToOccupy As Integer = 0

    Private _iCur As Integer = 0

    Public Sub New()

        Me.ResetCurrentIndex()

    End Sub

    Public ReadOnly Property Count() As Integer

        Get
            Return m.Count
        End Get

    End Property

    Public Sub ResetCurrentIndex()

        _iCur = 0

    End Sub

    Public ReadOnly Property CurrentIndex() As Integer
        Get
            Return _iCur
        End Get
    End Property

    Public Sub Add(ByVal uText As String, ByVal uImageGUID As String)

        Dim n As New udtWordImage
        n.GeneralImageGUID = uImageGUID
        n.Text = uText

        m.Add(n)

    End Sub
    Public Sub Update(ByVal uIndex As Integer, ByVal uText As String, ByVal uImageGUID As String)

        Dim n As New udtWordImage
        n.Text = uText
        n.GeneralImageGUID = uImageGUID

        m(uIndex) = n

    End Sub

    Public ReadOnly Property Text(ByVal uIndex As Integer) As String
        Get
            Return m(uIndex).Text
        End Get
    End Property

    Public ReadOnly Property GeneralImageGUID(ByVal uIndex As Integer) As String
        Get
            Return m(uIndex).GeneralImageGUID
        End Get
    End Property

    Public Sub RemoveAt(ByVal uIndex As Integer)

        m.RemoveAt(uIndex)

    End Sub

    Public Sub MoveUp(ByVal uIndex As Integer)

        m.move(uIndex, uIndex - 1)

    End Sub

    Public Sub ScrollForward()

        Dim lNewCur As Integer = _iCur + m_CountCellsToOccupy


        If lNewCur > m.Count - 1 Then
            lNewCur = 0
            If lNewCur < 0 Then
                lNewCur = 0
            End If
        End If

        _iCur = lNewCur

    End Sub
    Public Sub ScrollToStart()

        _iCur = 0

    End Sub
    Public Sub ScrollToEnd()

        _iCur = (m.Count - 1) - m_CountCellsToOccupy

        If _iCur < 0 Then
            _iCur = 0
        End If

    End Sub
    Public Sub ScrollBackward()

        _iCur = _iCur - m_CountCellsToOccupy

        If _iCur < 0 Then
            _iCur = 0
        End If

    End Sub


    Public WriteOnly Property CountCellsToOccupy() As Integer
        Set(value As Integer)
            m_CountCellsToOccupy = value
        End Set
    End Property

    Public Sub MoveDown(ByVal uIndex As Integer)

        m.move(uIndex, uIndex + 1)

    End Sub
    Public Function ShallowCopy() As clsPageWordList

        Return DirectCast(Me.MemberwiseClone(), clsPageWordList)

    End Function
End Class

结构定义如下:

Public Structure udtWordImage
    Dim Text As String
    Dim GeneralImageGUID As String
    Dim WordListIndex As Integer
End Structure

现在,编译器抱怨这一行

m.move(uIndex, uIndex - 1)

错误是“ Move不是List(Of udtWordImage)的成员”。

奇怪的是,如果我右键单击Move方法并选择“转到定义”,它将带我到ExtensionMethods子项。

这里出了什么问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

忘记将模块标记为公开了。

这样更改即可解决问题:

Public Module ExtensionMethods