簡體   English   中英

包含列表未正確初始化的結構列表

[英]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並返回與RecordType的Focus相關聯的Records列表。

我想打印所有RecordType及其關聯的記錄,但是當我遍歷記錄類型時,列表中沒有任何內容。

我不確定我是完全做錯了,還是錯過了一些簡單的事情。

任何幫助將不勝感激。

謝謝

我看不到您的所有代碼,因此我在這里猜測。 結構不像類。 當您在方法中傳遞它們時,該方法將具有副本而不是引用。

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

最簡單的解決方案可能是使用類而不是結構。

這種情況很可能會引起問題...

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實例。 在像這樣的循環中將項目添加到列表中時,您需要在每次循環迭代時創建該項目的實例。 修改並重新添加同一實例可能會產生意外行為。

取而代之的是,您可能想像這樣來構造它:

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變量重構為更高的范圍,從語義和結構上講,像這樣在每個循環中創建一個新變量就更加清楚和安全。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM