【问题标题】:Collection of Classes - Array property not populating correctly类的集合 - 数组属性未正确填充
【发布时间】:2013-12-12 00:08:51
【问题描述】:

我正在将一个 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 行)

【问题讨论】:

    标签: arrays vb.net class collections


    【解决方案1】:

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

    【讨论】:

    • 克里斯-谢谢!刚刚将 dim 语句移到 do 循环中,正如您所说 - 现在运行完美。只是为了让我明白为什么......这段代码在 vb6 中是相同的 - 为什么在那个旧的 vb 版本中不需要以这种方式创建数组?只是认为原因可能对其他转换障碍有益......无论如何 - 感谢您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 2011-08-09
    • 2012-03-19
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2015-09-22
    相关资源
    最近更新 更多