包含List未正确初始化的Struct列表

时间:2015-02-06 19:58:43

标签: .net vb.net list

我有以下属性:

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关联的记录列表。

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

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

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

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变量重新分解到更高的范围,从语义上和结构上来说,在每个循环中创建一个新变量就更加清晰和安全。