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





mcaincAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.