我正在將vb6程序遷移到vb.net。基本上下面的函數是創建一個類的集合。我使用該鍵作爲test_id來訪問其他地方集合中的各個類。我遇到的唯一問題是該類的屬性Sieves將隨着集合中的每個類的添加而更新。換句話說 - 當我在第一個循環/第一個類上執行時 - 我將sngSieveArray分配給我的類的屬性(.sieves)。一切都很好,數據正是數據庫包含的內容。在第二個循環中 - 只要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排除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 row(s)affected)
chris - 謝謝!只是將暗淡的聲明轉移到do循環中,如您所述 - 現在完美運行。只是爲了讓我能理解爲什麼......這段代碼在vb6中是相同的 - 爲什麼不需要在這個較舊的vb版本中以這種方式創建數組?只是認爲其原因可能對其他轉換障礙有益......不管 - 感謝soln。 – user3093316