Class 在 VB 中列出了 class

Class with a list of class in VB

我正在尝试编写一个 DLL,它将接受带有(T 的)嵌套列表的自定义输入

我创建了 2 个 classes,一个 Main Class ClassPolyPoints 和嵌套的 Class classEF

我在尝试将(classEF 的)列表对象传递到 ClassPolyPoints 对象时出错。

Dim TMP_effPoints As New List(Of classEF)

  For i = 0 to 10
        TMP_effPoints.Add(New classEff(
                 i,
                 i*0.125
                 ))
    Next


Dim tmpClass As New ClassPolyPoints(9.8765, TMP_effPoints)

Class 包含嵌套列表(classEF)

Public Class ClassPolyPoints
Sub New(ByVal x_P0 As Double,
        ByVal x_EffPoints As List(Of classEF))

    _P0 = x_P0

    With _effPoints
        For Each a In x_EffPoints
            .Add(New classEFF(
                 a.ID,
                 a.Eff
                 ))
        Next
    End With

End Sub

Private _effPoints As List(Of classEff)
Public Property effPoints() As List(Of classEff)
    Get
        Return _effPoints
    End Get
    Set(ByVal value As List(Of classEff))
        _effPoints = value
    End Set
End Property


Private _P0 As Double
Public Property P0() As Double
    Get
        Return _P0
    End Get
    Set(ByVal value As Double)
        _P0 = value
    End Set
End Property

End Class

嵌套class

Public Class classEF
Sub New(X_ID As Integer, X_Eff As Double)

    _ID = X_ID
    _Eff = X_Eff

End Sub

Private _ID As Integer
Public Property ID() As Integer
    Get
        Return _ID
    End Get
    Set(ByVal value As Integer)
        _ID = value
    End Set
End Property
Private _Eff As Double
Public Property Eff() As Double
    Get
        Return _Eff
    End Get
    Set(ByVal value As Double)
        _Eff = value
    End Set
End Property

End Class

你不需要在这里循环,只需分配列表即可。

Public Class ClassPolyPoints
  Sub New(ByVal x_P0 As Double,
    ByVal x_EffPoints As List(Of classEF))
   Me._P0 = x_P0
   Me.effPoints = x_EffPoints
 End Sub
 '''

问题是您在尝试将 class classEF:

的新元素添加到列表之前忘记创建 _effPoints 实例

即在class ClassPolyPoints中,你必须更改以下声明:

Private _effPoints As List(Of classEff)

进入这个:

Private _effPoints As New List(Of classEF)