dim将数据从较低的变量拉到较高的变量

时间:2017-09-08 06:50:08

标签: vb.net

我有

 Dim m_LedgerList As New Ejm.Financial.Entities.LedgerList
    m_LedgerList = My.StaticData.LedgerList
    m_LedgerList.Filter = "LedgerfunctionID = 2"

但我把m_LedgerList.Filter上的过滤器拉到了My.StaticData.LedgerList

任何想法如何阻止过滤器上升?

2 个答案:

答案 0 :(得分:1)

这一行

m_LedgerList = My.StaticData.LedgerList

使变量 m_LedgerList 引用变量 My.StaticData.LedgerList 引用的相同数据。这不是副本,这只是两个查看内存中相同数据的变量。因此,您对 m_LedgerList 变量执行的任何操作都会对 My.StaticData.LedgerList 所看到的相同数据起作用。

如果您想拥有不同的数据集,则需要在新的内存位置复制原始数据。这可以在LedgerList类中完成,类​​似这样

Public Class LedgerList
     Public Function Duplicate() As LedgerList

           ' This create a new memory area for a new LedgerList
           Dim result = new LedgerList()

           ' Code to duplicate and add the elements in this 
           ' class to the new list

           return result                   
     End Function
End Class

现在你可以选择

m_LedgerList = My.StaticData.LedgerList.Duplicate()
m_LedgerList.Filter = "LedgerfunctionID = 2"

答案 1 :(得分:0)

您可以按如下方式实施ICloneable

Class LedgerList
  Implements ICloneable

  Property Property1 As String
  Property Filter As String

  Public Function Clone() As Object Implements ICloneable.Clone
    Dim myclone As New LedgerList
    myclone.Property1 = Me.Property1
    myclone.Filter = Me.Filter
    Return myclone
  End Function
End Class

然后复制你的对象:

 Dim m_LedgerList As Ejm.Financial.Entities.LedgerList = DirectCast(My.StaticData.LedgerList.Clone, Ejm.Financial.Entities.LedgerList)
 m_LedgerList.Filter = "LedgerfunctionID = 2"