VB替换列表中的对象

问题描述 投票:0回答:1

我有2个已批准供应商和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.net list
1个回答
1
投票

您不是通过影响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
© www.soinside.com 2019 - 2024. All rights reserved.