• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

VB.NET ArrayList of Structure, aList.Remove(myStructData) does not remove anything

I have a public structure called "pMessages" i also have an arraylist that is made up of multiple entries of this structure

I am trying to do something like:

Dim PM as pMessages

For x = aList.count - 1 to 0 Step -1
    PM = aList(x)
    ... some work
     RemoveFromPMList(PM)
Next

Private Sub RemoveFromPMList(ByVal PMEntry as pMessages)
    aList.Remove(PMEntry)
End Sub


but nothing gets removed, am I doing something wrong?


The problem is, nothing is removed from the arraylist





0
mcainc
Asked:
mcainc
1 Solution
 
BinuthCommented:
your working perfectly here
can you provide full code ? lkie.. aList declaration , assign... etc
0
 
Praveen VenuTechnical Project ManagerCommented:
Can u pls try this



For x = aList.count - 1 to 0 Step -1
Dim PM as pMessages
    PM = aList(x)
    ... some work
     RemoveFromPMList(PM)
Next

Private Sub RemoveFromPMList(ByRef PMEntry as pMessages)
    aList.Remove(PMEntry)
End Sub

---------------------------------------------------
mark as solution if it works
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The problem is in your very first FIVE words:

     "I have a public structure"

A STRUCTURE is a VALUE type...thus whenever you pass a Structure it gets COPIED.  This is why nothing ever gets removed...because the COPY doesn't exist in your ArrayList!

See:
http://msdn.microsoft.com/en-us/library/2hkbth2a(VS.80).aspx

    "Assigning Variables. When you assign one structure variable to another, or pass a structure instance to a procedure argument, the current values of all the variable elements are copied to the new structure. When you assign one object variable to another, or pass an object variable to a procedure, only the reference pointer is copied."

If you want to remove a specific Structure from your ArrayList you will have to manually iterate over all items in there and manually check for "equality":

    "Equality. Equality testing of two structures must be performed with an element-by-element test."

If you change from a Structure to a Class then you'll have a different story on your hands....  =)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mcaincAuthor Commented:
ok so lemmie get this straight

Public Module Common
    Public Structure pMessages
        Dim user As String
        Dim message As String
    End Structure
End Module

vs

Public Class pMessages
    Public user As String
    Public message As String
End Class

The ArrayList would be able to remove the class object & I'm assuming this may be better for memory management?

So...

Dim PM as New pMessages

For x = aList.count - 1 to 0 Step -1
    PM = aList(x)
    ... some work
     RemoveFromPMList(PM)
Next

Should work?

Thanks Idle, you've been a life saver the last few days!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That should work...though you don't need the "New" in there as you assigning PM to an instance from the ArrayList.

Another thought...you are performing an action on each pMessage and removing it from the List with an end result of the ArrayList being empty.  Why not just perform the action on each pMessages and then empty the ArrayList OUTSIDE the loop after you're done?
0
 
mcaincAuthor Commented:
I didn't do it when outside of the loop because some of the conditions would require me to keep an object in there until a timestamp is met...

i thought about populating an "aListKeepers" arraylist during the loop with what to keep & then copying it back over the original after the loop was done.. do you think this would be a better idea and is it as simple as doing aList = aListKeepers?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now