如何为自定义对象列表构造 "Add" 例程?

How do I construct an "Add" routine for a custom object list?

我觉得问这个问题真的很愚蠢,但是……

我正在尝试在 VB 中创建一个自定义对象,它本身就是自定义对象的列表(或集合,或“元组”——我不确定它们之间的区别是什么) ,我需要创建例程来添加和删除这些次要对象 to/from 较大的自定义对象。到目前为止,我的代码是这样的:

Public Class parameterSet
    Friend _xParameter As String
    Public Property xParameter() As String
        Get
            Return _xParameter
        End Get
        Set(value As String)
            _xParameter = value
        End Set
    End Property
    Friend _yParameter As String
    Public Property yParameter() As String
        Get
            Return _yParameter
        End Get
        Set(value As String)
            _yParameter = value
        End Set
    End Property
    Friend _zParameter As String
    Public Property zParameter() As String
        Get
            Return _zParameter
        End Get
        Set(value As String)
            _zParameter = value
        End Set
    End Property
    Public Sub New(ByVal xParameter As String, ByVal yParameter As String, ByVal zParameter As String)
        _xParameter = xParameter
        _yParameter = yParameter
        _zParameter = zParameter
    End Sub
End Class
Public Class parameterCollection
    Friend _parameterCollection As New List(Of parameterSet)
    Friend Sub Add(xParameter As String, yParameter As String, zParameter As String)
        Throw New NotImplementedException()
    End Sub
End Class

我必须在 Add 例程中放入什么才能使它工作?

您的第一个 class 应该如下所示:

Public Class ParameterSet

    Public Property X As String
    Public Property Y As String
    Public Property Z As String

    Public Sub New(x As String, y As String, z As String)
        Me.X = x
        Me.Y = y
        Me.Z = z
    End Sub

End Class

您的第二个 class 应该如下所示:

Imports System.Collections.ObjectModel

Public Class ParameterSetCollection
    Inherits Collection(Of ParameterSet)

    Public Overloads Sub Add(x As String, y As String, z As String)
        Add(New ParameterSet(x, y, z))
    End Sub

End Class

您甚至可能想这样做:

Imports System.Collections.ObjectModel

Public Class ParameterSetCollection
    Inherits Collection(Of ParameterSet)

    Public Overloads Function Add(x As String, y As String, z As String) As ParameterSet
        Dim item = New ParameterSet(x, y, z)

        Add(item)

        Return item
    End Function

End Class

Collection(Of T) class 已经提供了所有标准的集合功能,您可以根据需要对其进行扩展。