包含列表的结构列表未正确初始化

List of Struct that contains List not initializing correctly

我有以下属性:

Protected y As New RecordType
Protected x As New Record
Protected ListOfRecordTypes As New List(Of RecordType)

我有以下结构:

Public Structure Record
    Public RecordType As String
    Public Location As String
End Structure

Public Structure RecordType
    Public Focus As String
    Public Records As List(Of Record)
End Structure

我循环遍历并使用此函数将一堆 RecordType 添加到 ListofRecordTypes 列表中:

Private Sub GetFocus()

        Dim CompData As SqlDataReader


        Dim connStringOOC = CONNSTRING2
        Dim qry As String = "SELECT DISTINCT Focus FROM dbo.Table"

        Using conn As New SqlConnection(connStringOOC)
            Dim cmd As New SqlCommand(qry)
            cmd.Connection = conn
            conn.Open()
            CompData = cmd.ExecuteReader()
            Try
                Do While CompData.Read()
                    y.Focus = CompData("Focus")
                    ListOfRecordTypes.Add(y)
                Loop

            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Using
    End Sub

完成后,我想调用此函数:

Private Function GetRecordType(ByVal focus As String) As List(Of Record)
        Dim CompData As SqlDataReader
        Dim x As New Record
        Dim y As New List(Of Record)
        Dim connStringOOC = CONNSTRING

        Dim qry As String = "SELECT DISTINCT RecordType FROM dbo.Table WHERE Focus = @Focus"

        Using conn As New SqlConnection(connStringOOC)
            Dim cmd As New SqlCommand(qry)
            cmd.Connection = conn
            conn.Open()
            cmd.Parameters.AddWithValue("@Focus", focus)
            CompData = cmd.ExecuteReader()
            Try
                Do While CompData.Read()
                    x.RecordType = CompData("RecordType")
                    y.Add(x)
                Loop

            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Using
        Return y
    End Function  

此函数应该遍历 ListOfRecordTypes 和 return 与 RecordType 的焦点关联的记录列表。

我想打印所有 RecordType 及其相关记录,但是当我遍历记录类型时,列表中没有任何内容。

我不确定我是否完全做错了,或者我是否错过了一些简单的事情。

如有任何帮助,我们将不胜感激。

谢谢

我没有看到你所有的代码,所以我在这里猜测一下。结构不像 class。当您在方法中传递它们时,该方法将有一个副本而不是引用。

Structure Test
    Public Value As String
End Structure

Sub Main()

    Dim a As New Test

    SetValues1(a)
    Console.WriteLine(a.Value) ' Empty

    SetValues2(a)
    Console.WriteLine(a.Value) ' prints 123

    Console.ReadLine()

End Sub

Sub SetValues1(ByVal b As Test)
    b.Value = "123"
End Sub

Sub SetValues2(ByRef b As Test)
    b.Value = "123"
End Sub

这是另一个使用列表的示例。从列表中获取对象后,我现在拥有该对象的副本。

Structure Test
    Public Value As String
End Structure

Sub Main()

    Dim x As Test
    Dim l As New List(Of Test)

    l.Add(New Test)
    x = l(0)
    x.Value = "123"

    Console.WriteLine(l(0).Value) ' Empty
    Console.WriteLine(x.Value) ' prints 123
    Console.ReadLine()

End Sub

最简单的解决方案可能是使用 classes 而不是结构。

这种情况很可能会导致问题...

Protected y As New RecordType

Private Sub GetFocus()
    ' other code

    Do While CompData.Read()
        y.Focus = CompData("Focus")
        ListOfRecordTypes.Add(y)
    Loop

    ' other code
End Sub

这是因为您每次都在重复使用相同的 y 实例。在这样的循环中将项目添加到列表时,您需要在循环的每次迭代中创建项目的 new 实例。修改并重新添加 相同的 实例可能会出现意外行为。

相反,您可能希望更像这样构建它:

Private Sub GetFocus()
    ' other code

    Do While CompData.Read()
        Dim y As New RecordType
        y.Focus = CompData("Focus")
        ListOfRecordTypes.Add(y)
    Loop

    ' other code
End Sub

没有必要将 y 变量重构到更高的范围,从语义和结构上来说,像这样在每个循环中创建一个新变量会更加清晰和安全。