locked
Generic method using delegates RRS feed

  • Question

  • Hi All,


    I have to create generic method that should return generic type.

    Class Form
    {
    int eid;
    string Ename;
    string joindate;
    }

    Class Course
    {
    int eid;
    string courseName;
    string joindate;
    }

    public void getvalue()
    {
    eid=1;joindate="10/05/2001";

    public List<Form> LstFrm= new List<Form>();
    LstFrm.Add(new Form{eid=1,fname="AA",joindate="10/05/2001"});
    LstFrm.Add(new Form{eid=1,fname="AA",joindate="10/05/2001"});
    LstFrm.Add(new Form{eid=2,fname="AAE",joindate="11/05/2001"});

    GetDetails(LstForm,eid,joindate);
    public List<Course> Lstcourse= new List<Course>();
    Lstcourse.Add(new Form{eid=1,courseName="BA",joindate="10/05/2001"});
    Lstcourse.Add(new Form{eid=1,courseName="BA",joindate="10/05/2001"});
    Lstcourse.Add(new Form{eid=2,courseName="MBA",joindate="11/05/2001"});

    GetDetails(Lstcourse,eid,joindate);

    }


    Can anyone explain how to write GetDetails method that should return list of object as generic type


    SaranRam

    Tuesday, November 12, 2013 9:55 AM

Answers

  • Create an empty interface and let both of your classes implement that,

    public interface ICommon { }
    
    class Form : ICommon           class Course : ICommon
    {                              {
       int eid;                         int eid;
       string Ename;                    string Ename;
       string joindate;                 string joindate;
    }                              }

    Then you can write you GetDetails method as,

    public List<T> GetDetails<T>() where T:ICommon
    {
        ...
    }

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, November 12, 2013 3:47 PM

All replies

  • Hi Saranram, 

         You want your function "GetDetails" should return "Form" type when you pass "LstForm" and 'Course" type when you pass "LstCourse" ??


    Satya R Biswal

    Tuesday, November 12, 2013 10:28 AM
  • Right now you are returing a void.  Use object which can be cast to most other types and other types ca be cast to an object

    From : public void getvalue()

    To : public object getvalue()



    jdweng

    Tuesday, November 12, 2013 11:06 AM
  • Yes Satya,


    SaranRam

    Tuesday, November 12, 2013 11:25 AM
  • Ok You can return type of Object and later you can type cast that object . 

    Satya R Biswal

    Tuesday, November 12, 2013 11:43 AM
  • public List<T> GetDetails<T>(List<T> lst,int eid,string joindate)
    {
         //perform your task and return the list of object
    }

    You can try with the above code, and call as below -

    GetDetails<Form>(LstForm,eid,joindate);

    Avik Das

    Tuesday, November 12, 2013 12:01 PM
  • Create an empty interface and let both of your classes implement that,

    public interface ICommon { }
    
    class Form : ICommon           class Course : ICommon
    {                              {
       int eid;                         int eid;
       string Ename;                    string Ename;
       string joindate;                 string joindate;
    }                              }

    Then you can write you GetDetails method as,

    public List<T> GetDetails<T>() where T:ICommon
    {
        ...
    }

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, November 12, 2013 3:47 PM