If I have an ArrayList called holder: {2,3,3,5,4,7,1,7,8,4}. Let say I wanted to find the count of certain element's occurrence in the above array within a certain range, I have these function:
In VB.Net:
Private Function getValInRange(ByVal lowerVal As Integer, ByVal upperVal As Integer, ByVal holder As ArrayList) As Integer
Dim count As Integer = 0
For Each item As Integer In holder
If (item <= upperVal AndAlso item >= lowerVal) Then
count = count + 1
End If
Next
Return count
End Function
In C#:
private int getValInRange(int lowerVal, int upperVal, ArrayList holder)
{
int count = 0;
foreach (int item in holder)
{
if ((item <= upperVal && item >= lowerVal))
{
count = count + 1;
}
}
return count;
}
So, when I query count = getValInRange(3,5,holder), I shall get a return of 5
I know the above function will be able to satisfy my needs, but I wonder if there is already a built in function that I can use. I plan to clean up my code and learn at the same time. Thanks a lot...
ArrayList? That's been somewhat-obsolete since 2005. If you could use some implementation ofIEnumerable<int>(e.g.List<int>orint[]) it would be much simpler. I'd also suggest following .NET naming conventions - I'd call the methodCountValuesInRangefor example.CountValuesInRange