Filtering Array to return subset of elements in .Net 2.0
I have the following in a .Net 2.0 app:
Private chequeColl() As Cheque
For i = 0 To m.Length - 2
chequeColl(i) = New Cheque()
chequeColl(i).Id = Start_Mp + i
chequeColl(i).Status = m(i)
Next
I now want to make chequeColl only contain those items where Status is not equal to 41. 开发者_JS百科In LINQ this would be easy but I can't think how to do it.
I cannot use the .Net 2.0 LINQ bridge products, I have to do this the old school way. At the end of it chequeColl must only contain those items that are not status of 41. I cannot have empty elements.
If it's .Net 2.0, you can use generics.
How about putting your collection in a List<T>
and then using FindAll
?
Here's a pretty good example that walks through it for you.
And here's another, longer one.
The essence of the first example (you should really read the article):
Public Class Person
Public age As Integer
Public name As String
Public Sub New(ByVal age As Integer, ByVal name As String)
Me.age = age
Me.name = name
End Sub 'New
End Class 'Person
List<person>people = new List<person>();
people.Add(New Person(50, "Fred"))
people.Add(New Person(30, "John"))
people.Add(New Person(26, "Andrew"))
people.Add(New Person(24, "Xavier"))
people.Add(New Person(5, "Mark"))
people.Add(New Person(6, "Cameron"))
'' Find the young
List<person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
Private chequeCollCleansed() as Cheque
Private count as int = 0
For i = 0 To chequeColl.Length
If chequeColl[i].Status != 41 Then
chequeCollCleansed(count) = chequeColl[i]
count = count + 1
End If
Next
Set chequeColl = chequeCollCleansed
Please excuse my Basic, treat as pseudo code!
edit having seen the generics suggestion i now realise how old skool this is!
Don't add them in the first place:
Private chequeColl() As Cheque
For i = 0 To m.Length - 2
If m(i) != 41 Then
chequeColl(i) = New Cheque()
chequeColl(i).Id = Start_Mp + i
chequeColl(i).Status = m(i)
End If
Next
精彩评论