如何获取集合中对象的索引

how to get the Index of object in collection

我正在尝试制作一个应用程序,在这个应用程序中,我有一个 List(of T) 包含一个对象的集合。

处理对象时,我需要知道它是列表中的索引。

示例:

Public Class

    Public oList as New List(of TestObject)
    Private Sub Test()


         Dim NewObject As New TestObject
         oList.add(NewObject)

         Index(NewObject)

    End Sub

    Private Sub Index(Byval TestObject As TestObject)

         debug.print(Testobject.index)

    End Sub
End Class

这样的事情可能吗?我在前段时间使用的参考文件中看到它可用,但现在我想在我自己的 class.

中提供它

有人可以提供样品吗?

PS:我知道我可以使用 List(Of T).IndexOf Method (T) 获取索引,但为了将来的可能性,我想从对象本身进行调用。

看起来像这样

Public oList As New List(Of TestObject)
Private Sub Test()

    Dim NewObject As New TestObject(oList.Count)
    oList.add(NewObject)

End Sub

Public Class TestObject
    Public index As Integer

    Public Sub New(IndxOfObj As Integer)
        Me.index = IndxOfObj
    End Sub
End Class

如果您一定需要将其作为对象上的 属性,我建议如下:

Public Class Main
  Public oList As New List(Of TestObject)
  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    Dim NewObject As New TestObject(Me)
    oList.Add(NewObject)
    Dim NewObject2 As New TestObject(Me)
    oList.Add(NewObject2)

    MsgBox(NewObject2.Index)
  End Sub

  Public Function Index(ByVal TestObject As TestObject) As Integer
    Return oList.IndexOf(TestObject)
  End Function
End Class

Public Class TestObject

  Private _main As Main
  Public ReadOnly Property Index() As Integer
    Get
        Return _main.Index(Me)
    End Get
  End Property

  Public Sub New(RootClass As Main)
    _main = RootClass
  End Sub

End Class

如果您碰巧将 Main class 作为 Singleton,则可以跳过整个发送 'Me' 到构造函数业务。然后你可以调用 Main.Index 而不将其存储为所有 TestObjects 上的 属性。

通常发生的情况是他们有一个自定义列表,他们不直接使用 List(Of T),而是在将项目添加到列表时将列表存储在对象中。

Module Module1

    Sub Main()

        Dim someList As New CustomList
        someList.Add(New CustomItem())
        someList.Add(New CustomItem())
        someList.Add(New CustomItem())

        Console.WriteLine(someList(1).Index)
        Console.ReadLine()

    End Sub

End Module

Class CustomItem

    ' Friend since we don't want anyone else to see/change it.
    Friend IncludedInList As CustomList

    Public ReadOnly Property Index
        Get
            If IncludedInList Is Nothing Then
                Return -1
            End If

            Return IncludedInList.IndexOf(Me)
        End Get
    End Property

End Class

Class CustomList
    Inherits System.Collections.ObjectModel.Collection(Of CustomItem)

    Protected Overrides Sub InsertItem(index As Integer, item As CustomItem)
        If item.IncludedInList IsNot Nothing Then
            Throw New ArgumentException("Item already in a list")
        End If

        item.IncludedInList = Me
        MyBase.InsertItem(index, item)
    End Sub

    Protected Overrides Sub RemoveItem(index As Integer)
        Me(index).IncludedInList = Nothing
        MyBase.RemoveItem(index)
    End Sub

End Class