By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

How do you trim an arraylist?

P: n/a
I have my own strongly typed collection class, and I added in this new
sub:

Public Sub TrimResults(ByVal iElementsToKeep As Integer)

Dim myArray() As clsMyClass

MyBase.InnerList.CopyTo(0, myArray, 0, iElementsToKeep)
MyBase.InnerList.Clear()
MyBase.InnerList.SetRange(0, myArray)

End Sub

...With the hope of getting the underlying innerlist to shed unwanted
elements at the end of it's list.

When run, I get this error on the .CopyTo line:

Array cannot be null. Parameter name: dest

Am I right in thinking that before I use the myArray variable, I have
to create a loop, and inside the loop, instantiate each and every
element of this array with a "new clsMyClass" line to make sure that
every element is ready to receive an element result from the CopyTo
operation?

Perhaps there's a good way of doing what I want to achieve, but my lack
of VB.NET knowledge is preventing me from seeing it.

Thank you, regards, dnw.

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Can you try this one.

Public Sub TrimResults(ByVal iElementsToKeep As Integer)
Dim myArray(iElementsToKeep-1) As clsMyClass
MyBase.InnerList.CopyTo(0, myArray, 0, iElementsToKeep)
MyBase.InnerList.Clear()
MyBase.InnerList.SetRange(0, myArray)

End Sub

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
Thanks very much Cor.

The working code is now:

Dim myArray(iElementsToKeep - 1) As clsSingleResult

MyBase.InnerList.CopyTo(0, myArray, 0, iElementsToKeep)
MyBase.InnerList.RemoveRange(0, MyBase.InnerList.Count)
MyBase.InnerList.AddRange(myArray)

Regards, dnw.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.