Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-13
6
Medium Priority
?
717 Views
Last Modified: 2012-05-05
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
Comment
Question by:mcainc
6 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22947978
your working perfectly here
can you provide full code ? lkie.. aList declaration , assign... etc
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 22948027
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22950633
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:mcainc
ID: 22969416
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 22969617
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
 

Author Comment

by:mcainc
ID: 22969632
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question