Search for custom types in ArrayList

Source: Internet
Author: User

In general, I only need to use the ArrayList. Contains () method to determine whether an element exists. However, if the ArrayList Contains a custom class, I need to implement it myself, as shown in the following example:

For example, we have a class:

public class Employee{     public int EmployeeID     {            get;set;     }     public string EmployeeName     {           get;set;     }     public DateTime BirthDate     {           get; set;     }
}

We need to find the Employee stored in the ArrayList. Define two enumerations first:

public enum MemType{    IntegerType = 1, StringType = 2, DateTimeType = 3, BooleanType = 4};public enum CmpOperator{    GreaterThan = 1, LessThan = 2, EqualTo = 3}

The specific implementation is as follows:

public static int Search(ArrayList objArr, string valueToSearch, string FieldName, MemType memType, CmpOperator comOp){    if (memType == MemType.StringType || memType == MemType.BooleanType)    {        comOp = CmpOperator.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 MemType.BooleanType:                if (Convert.ToBoolean(str) == Convert.ToBoolean(valueToSearch))                {                    return i;                }                break;            case MemType.DateTimeType:                switch (comOp)                {                    case CmpOperator.EqualTo:                        if (Convert.ToDateTime(str).CompareTo(Convert.ToDateTime(valueToSearch)) == 0)                        {                            return i;                        }                        break;                    case CmpOperator.GreaterThan:                        if (Convert.ToDateTime(str).CompareTo(Convert.ToDateTime(valueToSearch)) > 0)                        {                            return i;                        }                        break;                    case CmpOperator.LessThan:                        if (Convert.ToDateTime(str).CompareTo(Convert.ToDateTime(valueToSearch)) < 0)                        {                            return i;                        }                        break;                }                break;            case MemType.IntegerType:                switch (comOp)                {                    case CmpOperator.EqualTo:                        if (Convert.ToInt32(str) == Convert.ToInt32(valueToSearch))                        {                            return i;                        }                        break;                    case CmpOperator.GreaterThan:                        if (Convert.ToInt32(str) > Convert.ToInt32(valueToSearch))                        {                            return i;                        }                        break;                    case CmpOperator.LessThan:                        if (Convert.ToInt32(str) < Convert.ToInt32(valueToSearch))                        {                            return i;                        }                        break;                }                break;            case MemType.StringType:                if (str.Contains(valueToSearch))                {                    return i;                }                break;        }    }    return -1;}

The test code is as follows:

static void Main(string[] args) {     ArrayList al = new ArrayList();     Employee emp1 = new Employee();     emp1.EmployeeID = 2;     emp1.EmployeeName = "cary";                emp1.BirthDate = new DateTime(1982, 4, 1);     Employee emp2 = new Employee();     emp2.EmployeeID = 3;     emp2.EmployeeName = "james";                emp2.BirthDate = new DateTime(1983, 4, 1);     al.Add(emp1);     al.Add(emp2);     int searIndex = Search(al, "3", "EmployeeID", MemType.IntegerType, CmpOperator.EqualTo);     Console.WriteLine(searIndex);     Console.ReadLine();         }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.