类的集合 - 未正确填充的数组属性

时间:2013-12-12 00:08:51

标签: arrays vb.net class collections

我正在将vb6程序迁移到vb.net。基本上下面的函数是创建一个类的集合。我使用密钥作为test_id来访问其他地方的集合中的各个类。我唯一的问题是该类的属性Sieves正在更新,每次连续添加到集合中。换句话说 - 当我在第一个循环/第一个类上执行时 - 我将sngSieveArray分配给我的类的属性(.sieves)。一切都很好,数据正是db包含的内容。在第二个循环中 - 一旦sngsievarray填充了当前类的数据 - 第一类的筛选阵列就会更新为相同的数据。

总共 - 有3个类 - 所以每个.sieves属性都被分配了最后一个类的筛子阵列。

我完全不知道这是怎么回事 - 班级中的所有其他属性都会被保留,每个连续的班级都会被创建。

Public Function GetSieveTestsInLot(mix_app_id As Long, mix_lot_id As Long, Optional,combinedMixLot As Long = 0, Optional combinedMixLot2 As Long = 0, Optional boolExclude As boolean = False) As Collection
    On Error GoTo ErrorHandler
    Dim strsql As String
    Dim rsTests As SqlClient.SqlDataReader
    Dim mycmd As SqlClient.SqlCommand
    Dim mySieveTest As clsSieveTest
    Dim sngSieveArray(0 To 12) As Single

    Dim theConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(My.Settings.AsphaltConnectionString)
    If Not (theConn.State = ConnectionState.Open) Then
        theConn.Open()
    End If

    strsql = "select mix_lot.MIX_LOT_ID,LOT_NUMBER, EXCLUDED,SAMPLE_TEST_ID, mix_lot.MIX_APPLICATION_ID, p.* from " _
        & "(select TEST_PERCENT_PASSING.PER_PASSING, TEST_PERCENT_PASSING.SIEVE_ID, TEST_PERCENT_PASSING.TEST_ID from TEST_PERCENT_PASSING) as t " _
        & "pivot (min(per_passing) for Sieve_id in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13])) p inner join TEST_DATA_CALC on TEST_DATA_CALC.TEST_ID = p.TEST_ID " _
        & "inner join MIX_LOT on MIX_LOT.MIX_LOT_ID = TEST_DATA_CALC.MIX_LOT_ID where MIX_APPLICATION_ID = " & mix_app_id & " And (MIX_LOT.mix_lot_id = " & mix_lot_id & " or mix_lot.mix_lot_id = " & combinedMixLot _
    & " or mix_lot.mix_lot_id = " & combinedMixLot2 & ")"



    mycmd = New SqlClient.SqlCommand
    mycmd.CommandText = strsql
    mycmd.CommandType = CommandType.Text
    mycmd.Connection = theConn

    rsTests = mycmd.ExecuteReader

    GetSieveTestsInLot = New Collection
    If rsTests.HasRows Then
        Do While rsTests.Read
            If Not (boolExclude = True And rsTests!excluded = True) Then
                mySieveTest = New clsSieveTest
                With mySieveTest
                    .test_id = rsTests!test_id
                    .mix_lot_id = rsTests!mix_lot_id
                    .lot_number = rsTests!lot_number
                    .mix_app_id = mix_app_id
                    .sample_test_id = rsTests!sample_test_id
                    .test_exlcuded = rsTests!excluded

                    For i = 0 To 12
                        For j = 0 To rsTests.FieldCount - 1
                            '   MsgBox(rsTests.GetName(j).ToString)
                            If rsTests.GetName(j).ToString = (i + 1).ToString Then
                                '    MsgBox(rsTests.Item(j).ToString)
                                sngSieveArray(i) = rsTests.Item(j)
                            End If
                        Next j
                    Next i

                    .sieves = sngSieveArray
                End With

                GetSieveTestsInLot.Add(mySieveTest, CStr(mySieveTest.test_id))
            End If

        Loop
        rsTests.Close()
    End If

    rsTests = Nothing
    mycmd = Nothing
    theConn.Close()
    theConn = Nothing
    Exit Function

ErrorHandler:
    MsgBox("An error has occured in GetSieveTestsInLot in clsSieveTest with Error number: " & Err.Number & " defined as" & vbCrLf _
    & Err.Description, vbCritical, "Program Failure")
    rsTests = Nothing
    mycmd = Nothing
    theConn.Close()
    theConn = Nothing
End Function

MIX_LOT_ID LOT_NUMBER EXCLUDED SAMPLE_TEST_ID MIX_APPLICATION_ID TEST_ID 1 2 3 4 5 6 7 8 9 10 11 12 13


141 1 0 1 36 430 5.15 100 100 100 90.7 76.3 49.6 35.3 24.8 17.7 13.1 8.5 5.5 141 1 0 2 36 431 5.35 100 100 100 91.2 78.5 48.6 35.4 25 18.6 13.4 8 4.9 141 1 0 3 36 432 5.16 100 100 98.8 84.8 74.1 53.8 38.4 26.8 19 13.7 9.1 5.2

(3行受影响)

1 个答案:

答案 0 :(得分:0)

原因是因为数组是引用类型。 clsSieveTest的每个实例的.sieves属性都引用了数组的相同实例。你需要在你的Do While循环中移动sngSieveArray 的声明,以便你的clsSieveTest的每个新实例都有自己的数组。