VB替换列表中的对象

时间:2019-04-23 08:07:30

标签: vb.net list

我有2个已批准的供应商列表和originalSupplierData

填充批准的供应商后,我们会将条目克隆到originalSupplierData中。如果他们修改了记录但没有保存,我们询问用户是否要恢复更改。如果他们想还原,我尝试用原始数据的克隆替换批准的供应商中的条目。我当前的还原代码是

    Public Sub RevertChanges(SupplierID As Integer)

    Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single()
    Dim modifiedSupplier As Approved_Supplier = ApprovedSuppliers.Where(Function(x) x.ID = SupplierID).Single()

    modifiedSupplier = orignalSupplier.Clone


End Sub

modifiedSupplier将使用原始值进行更新,但是列表中的实际项目不会使用这些值进行更新。 如果我修改属性之一,列表将更新。我不确定我在做什么错,请问有人可以向我指出正确的方向吗?

编辑 从数据库填充列表的代码是

        supplierTableAdapter.Fill(supplierTable)

    _approvedSuppliers = New List(Of Approved_Supplier)
    originalSupplierlist = New List(Of Approved_Supplier)()

    For Each row As ApprovedSuppliersDataset.ApprovedSupplierRow In supplierTable
        supplier = New Approved_Supplier()

        supplier.supplierID = row.PLSupplierAccountID
        supplier.AccountNumber = row.SupplierAccountNumber
        supplier.SupplierName = row.SupplierAccountName
        supplier.SupplierAddress = CompileAddress(row)
        supplier.Phone = CompilePhoneNumber(row)

        If row.IsIDNull = False Then

            supplier.ID = row.ID

            If row.IsAdded_ByNull = False Then
                supplier.AddedBy = row.Added_By
            End If

            If row.IsApprovedNull = False Then
                supplier.Approved = row.Approved
            End If

            If row.IsAuditorNull = False Then
                supplier.Auditor = row.Auditor
            End If

            If row.IsAudit_CommentsNull = False Then
                supplier.AuditComments = row.Audit_Comments
            End If

            If row.IsAudit_DateNull = False Then
                supplier.AuditDate = row.Audit_Date
            End If

            If row.IsDate_AddedNull = False Then
                supplier.DateAdded = row.Date_Added
            End If

            If row.IsNotesNull = False Then
                supplier.Notes = row.Notes
            End If

            If row.IsQuestionnaire_Return_DateNull = False Then
                supplier.QuestionnaireReturnDate = row.Questionnaire_Return_Date
            End If

            If row.IsQuestionnaire_Sent_DateNull = False Then
                supplier.QuestionnaireSentDate = row.Questionnaire_Sent_Date
            End If

            If row.IsQuestionnaire_StatusNull = False Then
                supplier.QuestionnaireStatus = row.Questionnaire_Status
            End If

            If row.IsReplinNull = False Then
                supplier.Replin = row.Replin
            End If

            If row.IsReview_CommentsNull = False Then
                supplier.ReviewComment = row.Review_Comments
            End If

            If row.IsReview_DateNull = False Then
                supplier.ReviewDate = row.Review_Date
            End If

            If row.IsReviewerNull = False Then
                supplier.Reviewers = row.Reviewer
            End If

            If row.IsStakeholder_ContactNull = False Then
                supplier.StakeholderContact = row.Stakeholder_Contact
            End If

            If row.IsStandardsNull = False Then
                supplier.Standards = row.Standards
            End If

            If row.IsStandard_ExpiryNull = False Then
                supplier.StandardExpiry = row.Standard_Expiry
            End If

            If row.IsStatusNull = False Then
                supplier.Status = row.Status
            End If

            If row.IsSupplier_Expiry_DateNull = False Then
                supplier.SupplierExpiryDate = row.Supplier_Expiry_Date
            End If

            If row.IsSupplier_ScopeNull = False Then
                supplier.SupplierScope = row.Supplier_Scope
            End If

            If row.Is_T_CsNull = False Then
                supplier.TC = row._T_Cs
            End If
        End If

        supplier.ClearISDirty()
        _approvedSuppliers.Add(supplier)
        originalSupplierlist.Add(supplier.Clone)
    Next

对于克隆我们有

  Public Function Clone() As Object Implements ICloneable.Clone
    Dim cloned As New Approved_Supplier()

    cloned.ID = Me.ID
    cloned.DateAdded = Me.DateAdded
    cloned.Status = Me.Status
    cloned.AddedBy = Me.AddedBy
    cloned.Approved = Me.Approved
    cloned.AuditDate = Me.AuditDate
    cloned.Auditor = Me.Auditor
    cloned.AuditComments = Me.AuditComments
    cloned.QuestionnaireStatus = Me.QuestionnaireStatus
    cloned.QuestionnaireSentDate = Me.QuestionnaireSentDate
    cloned.QuestionnaireReturnDate = Me.QuestionnaireReturnDate
    cloned.ReviewDate = Me.ReviewDate
    cloned.Reviewers = Me.Reviewers
    cloned.ReviewComment = Me.ReviewComment
    cloned.Standards = Me.Standards
    cloned.StandardExpiry = Me.StandardExpiry
    cloned.SupplierScope = Me.SupplierScope
    cloned.Replin = Me.Replin
    cloned.TC = Me.TC
    cloned.Notes = Me.Notes
    cloned.StakeholderContact = Me.StakeholderContact
    cloned.SupplierExpiryDate = Me.SupplierExpiryDate

    cloned.supplierID = Me.supplierID 
    cloned.AccountNumber = Me.AccountNumber
    cloned.SupplierName = Me.SupplierName
    cloned.SupplierAddress = Me.SupplierAddress
    cloned.Phone = Me.Phone
    cloned.Email = Me.Email
    cloned.ClearISDirty()
    Return cloned

End Function

1 个答案:

答案 0 :(得分:1)

您不会通过影响ModifyedSupplier来替换列表中的内容。

尝试获取ModifyedSupplier的索引,然后用克隆替换找到的索引处的项目。

Public Sub RevertChanges(SupplierID As Integer)

    Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single()
    Dim modifiedIndex As Integer = ApprovedSuppliers.FindIndex(Function(x) x.ID = SupplierID)

    ApprovedSuppliers(modifiedIndex) = orignalSupplier.Clone()


End Sub