mcainc
asked on
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
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
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
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
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.... =)
"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.... =)
ASKER
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
can you provide full code ? lkie.. aList declaration , assign... etc