Click here to Skip to main content
16,006,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
I have two or more classes called Dashboard, Order, Shipment and etc. I have a function to get list of all modules where i will be use same classes.
For example,
C#
public class Dashboard
    {
        public string Key { get; set; }
        public string Name { get; set; }
    }
    public class Order
    {
        public string OrderNo { get; set; }
        public string Buyer { get; set; }
    }
    public class Shipment
    {
        public string ShipmentNo { get; set; }
        public string Location { get; set; }
    }

 public List<object> GetList()
        {
<!-- Get List Code Here-->return lstobject;
        }

Is there possible to call GetList() method and return the wanted list object. For example, if i want dashboard list then return List<Dashboard> If i want order list then return List<Order>.

Please anyone help me to use this type of method.
Posted
Updated 27-Dec-10 23:22pm
v2
Comments
TweakBird 28-Dec-10 5:22am    
Edited for code blocks formatting.
Espen Harlinn 12-Jan-11 4:18am    
And how will GetList "know" what you wnat it to return?

The below code will verify if the object in list is dashboard, it will create the object of Dashboard class. The same can be applied on other classes too.

public static List<object> GetList()
{
   // Get List Code Here-->
   List<object> lstobject = new List<object>();
   lstobject.Add(new Dashboard());

   return lstobject;
}

To get the object use the following block
List<object> lstObject = GetList();
Dashboard ds=null;
for (int i = 0; < lstObject.Count; i++)
{
    if (lstObject[i] is Dashboard)
    {
        ds = (Dashboard)lstObject[i];
        Console.WriteLine(ds.Key);
    }
}

Hope this answer your question
 
Share this answer
 
v3
Hi,

You can Use Like this.

public class getListClass
    {
        public static IList   getList()
        {
            List<Shipment> _lst = new List<Shipment>();
            Shipment _sh = new Shipment();
            _lst.Add(_sh);
            return _lst;             

        }
    }


List<Shipment> _ss = (List<Shipment>)getListClass.getList();


I have declare static only for test. You can use IEnumerable also instead of IList.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900