开发者

vb.net 2003 Arraylist of structure which have an arraylist

I have two structures

Public Structure myResearchData

    Public batchName As String
    Public arraylistRData As ArrayList
End Structure

Public Structure myResearchSubData
    Public researchDescription As String
    Public recordingDate As Date
    Public book As String
    Public page As String
    Public year As String
    Public fee As String
End Structure

I initialize them in a sub

Dim MyResearchDataAList As New ArrayList Dim MyResearchData As myResearchData MyResearchData.arraylistRData = New ArrayList Dim MyResearchSubData As myResearchSubData

I have an arraylist of myResearchSubData which is MyResearchData.arraylistRData and added it inside MyResearchDataAList. But when I cleared MyResearchData.arraylistRData the arraylist inside MyResearchDataAList is also cleared. I thought that once it is added to MyResearchDataAList it will hold the arraylist but it is also clered. Below is the process that I have done.

MyResearchSubData.recordingDate = Date.Parse(Date) MyResearchSubData.book = Book MyRe开发者_如何转开发searchSubData.page = Page MyResearchSubData.year = Year MyResearchSubData.fee = Fee

Put data in the structure of MyResearchSubData

MyResearchData.arraylistRData.Add(MyResearchSubData)

Added it in MyResearchData.arraylistRData

MyResearchDataAList.Add(MyResearchData)

Added it in MyResearchDataAList

MyResearchData.arraylistRData.Clear()

Cleared MyResearchData.arraylistRData for new data to be put in but it also clears the arraylist inside MyResearchDataAList and didn't old the contents of the arraylist

Thanks in advance for those who can help me with this problem


That is happening because it is actually the same items being added to the array list, as they are added by reference. You want to actually add a COPY of each item to your arrayList.

I generally find the easiest way to do something like that is to add a clone of the object during a loop.

To do that you may need to implement ICloneable to get the proper copy. At the level of the myResearchSubData however, it appears that you can just use the memberwiseClone Method.

If you change the to a class instead of a structure you can use the clone like this:

   Public Class myResearchSubData
        Implements ICloneable

        Public researchDescription As String
        Public recordingDate As Date
        Public book As String
        Public page As String
        Public year As String
        Public fee As String

        Public Function Clone() As Object Implements System.ICloneable.Clone
            Return Me.MemberwiseClone
        End Function
    End Class

Then you would want to loop through your original list of myResearchSubData and add its clone to the second list. Something like:

For Each item as myResearchSubData in MyResearchData.arraylistRData
   MyResearchDataAList.Add(CType(item.Clone, myResearchSubData))
Next

If you want to continue to use the structures then I would use the same loop type and make a function that creates a new myResearchSubData and copies the data from the original over to the new one.

  For Each item as myResearchSubData in MyResearchData.arraylistRData
       MyResearchDataAList.Add(CloneStructure(item))
    Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜