首页 > 解决方案 > VB替换列表中的对象

问题描述

我有 2 个列表 approvedSuppliers 和 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

标签: vb.netlist

解决方案


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

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

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

推荐阅读