Introduction
I was working on a database oriented application and came to know that I needed a code snippet that can search in an ArrayList
. Obviously, if you have stored defined data types directly, it is very easy to search the ArrayList
. That is by using the ArrayList.Contains
methods. In my case, I had an ArrayList
that contained objects of one of my own classes. The .NET Framework doesn't provide such a method to search within custom objects of particular classes. So I worked out a search method that can be used effectively to search within an ArrayList
.
Using the Code
You will need to include this method in your code in order to use it.
First of all, we need to create two enums to manage the datatypes and comparisons:
public enum MemberType
{
IntegerType = 1, StringType = 2, DateTimeType = 3, BooleanType = 4
};
public enum ComparisonOperator
{
GreaterThan=1,LessThan=2,EqualTo=3
}
Let's create an Employee
class. It may be the class you will use and search objects of:
public class Employee
{
private int e_id;
private string e_name;
private DateTime e_dob;
private bool e_isfemale;
public int EmployeeID
{
get { return e_id; }
set { e_id = value; }
}
public string EmployeeName
{
get { return e_name; }
set { e_name = value; }
}
public DateTime DateOfBirth
{
get { return e_dob; }
set { e_dob = value; }
}
public bool IsFemale
{
get { return e_isfemale; }
set { e_isfemale = value; }
}
}
And here is the Search
method:
public int Search(ArrayList objArr, string valueToSearch,
string FieldName, MemberType memType,ComparisonOperator comOp)
{
if (memType == MemberType.StringType || memType == MemberType.BooleanType)
{
comOp = ComparisonOperator.EqualTo;
}
for (int i = 0; i < objArr.Count; i++)
{
Type t = objArr[0].GetType();
System.Reflection.FieldInfo[] arrInner = t.GetFields();
System.Reflection.PropertyInfo PI = t.GetProperty(FieldName);
string str = PI.GetValue(objArr[i], null).ToString();
switch(memType)
{
case MemberType.BooleanType:
if (Convert.ToBoolean(str) == Convert.ToBoolean(valueToSearch))
{
return i;
}
break;
case MemberType.DateTimeType:
switch (comOp)
{
case ComparisonOperator.EqualTo:
if (Convert.ToDateTime(str).CompareTo(
Convert.ToDateTime(valueToSearch)) == 0)
{
return i;
}
break;
case ComparisonOperator.GreaterThan:
if (Convert.ToDateTime(str).CompareTo(
Convert.ToDateTime(valueToSearch)) > 0)
{
return i;
}
break;
case ComparisonOperator.LessThan:
if (Convert.ToDateTime(str).CompareTo(
Convert.ToDateTime(valueToSearch)) < 0)
{
return i;
}
break;
}
break;
case MemberType.IntegerType:
switch (comOp)
{
case ComparisonOperator.EqualTo:
if (Convert.ToInt32(str) == Convert.ToInt32(valueToSearch) )
{
return i;
}
break;
case ComparisonOperator.GreaterThan:
if (Convert.ToInt32(str) > Convert.ToInt32(valueToSearch))
{
return i;
}
break;
case ComparisonOperator.LessThan:
if (Convert.ToInt32(str) < Convert.ToInt32(valueToSearch))
{
return i;
}
break;
}
break;
case MemberType.StringType:
if (str.Contains(valueToSearch))
{
return i;
}
break;
}
}
return -1;
}
To use the above search function, you will need to do some thing like this:
ArrayList objArr = new ArrayList();
Employee objEmp = new Employee();
objEmp.EmployeeID = 1;
objEmp.EmployeeName = "Usman";
objEmp.IsFemale = false;
objEmp.DateOfBirth = new DateTime(1982,4,1);
objArr.Add(objEmp);
int searchedIndex = Search(objArr, "1", "EmployeeID",
MemberType.IntegerType, ComparisonOperator.EqualTo);
objArr
: It is the ArrayList
in which you are storing your objects.valueToSearch
: It is the value which you are trying to search in your list. Remember, you will always pass a string.FieldName
: The field you want to search.memType
: The DataType of the class member.comOp
: The comparison operator that you want to apply on search.
Points of Interest
In this snippet of code, the major part is the System.Reflection
namespace usage. This search method can also be extended to support other Data Types.