0
任何人都可以解釋爲什麼下面的代碼中的方法1不會改變其他2種方法所在的DataTable?解釋爲什麼DataRow沒有改變
這顯然是某種引用問題,但爲什麼呢?
Imports System
Imports System.Data
Public Class Test
Public Shared Sub Main()
'Build Table
Dim dt as New DataTable
dt.Columns.Add("ID",GetType(String))
dt.Columns.Add("Name",GetType(String))
'Populate Table
Dim dr as DataRow
dr = dt.NewRow()
dr("ID")="Man" : dr("Name") = "mike" : dt.Rows.Add(dr)
dr = dt.NewRow()
dr("ID")="Man" : dr("Name") = "ian" : dt.Rows.Add(dr)
dr = dt.NewRow()
dr("ID")="Man" : dr("Name") = "rob" : dt.Rows.Add(dr)
dr = dt.NewRow()
dr("ID")="Woman" : dr("Name") = "ann" : dt.Rows.Add(dr)
dr = dt.NewRow()
dr("ID")="Woman" : dr("Name") = "sam" : dt.Rows.Add(dr)
output(dt) 'Output Table
Dim drFilters() as DataRow = dt.Select("ID='Man'") 'Select all Man
'Method 1 does not change dt
'dr = dt.NewRow()
'dr("ID")="cowman" : dr("Name")="bugle"
'drFilters(1)=dr
'Method 2 does change dt
dr = drFilters(1)
dr("ID")="cowman" : dr("Name")="bugle"
'Method 3 does change dt
'drFilters(1)("ID")="cowman" : drFilters(1)("Name")="bugle"
output(dt) 'Output final table
End Sub
Public Shared Sub output(dt as DataTable)
for each dr as DataRow in dt.Rows
Console.WriteLine(dr("ID") + vbTab + dr("Name"))
Next
Console.WriteLine("")
End Sub
End Class
道歉愚蠢的測試數據:)
+1謝謝,這是一個很好的答案!現在很清楚:) –
很好的答案。我明白它是如何工作的,而且我現在仍然認爲我現在更好。 :) – Chris