tags:

views:

97

answers:

4

hi,guy, i want write a function using c#, and it accept any type parameter,i want use generic list to do, but i can't finish, it's wrong, how to do it? perhaps there are other ways??

thinks!

public class City
{
    public int Id;
    public int? ParentId;
    public string CityName;
}

public class ProductCategory
{
    public int Id;
    public int? ParentId;
    public string Category;
    public int Price;
}

public class Test
{

        public void ReSortList<T>(IEnumerable<T> sources, ref IEnumerable<T> returns, int parentId)
        {
           //how to do like this:
           /*
            var parents = from source in sources where source.ParentId == parentId && source.ParentId.HasValue select source;

            foreach (T child in parents)
            {
                returns.Add(child);
                ReSortList(sources, ref returns, child.Id);
            }
           */
        }

        public void Test()
        {
            IList<City> city = new List<City>();

            city.Add(new City() { Id = 1, ParentId = 0, CityName = "China" });
            city.Add(new City() { Id = 2, ParentId = null, CityName = "America" });
            city.Add(new City() { Id = 3, ParentId = 1, CityName = "Guangdong" });

            IList<City> results = new List<City>();
            ReSortList<City>(city, ref results, 0);  //error
        }
}
+2  A: 

You can't Add on IEnumerable you have to provide an output type that supports this operation such as IList.

Moreover, you can't access a Property ParentId on the type T, which is unknown in the scope of your method.

[EDIT] This may help you :

public class City
{
    public int Id;
    public int? ParentId;
    public string CityName;
}

public class ProductCategory
{
    public int Id;
    public int? ParentId;
    public string Category;
    public int Price;
}

public class Test
{

    public void f()
    {
        List<City> city = new List<City>();

        city.Add(new City() { Id = 1, ParentId = 0, CityName = "China" });
        city.Add(new City() { Id = 2, ParentId = null, CityName = "America" });
        city.Add(new City() { Id = 3, ParentId = 1, CityName = "Guangdong" });
        int searchedId = 0;
        IList<City> results = city.FindAll(delegate(City c)
                                               {
                                                   return c.ParentId.HasValue &&
                                                          c.ParentId == searchedId;
                                               });
    }
}
Seb
I also agree with Robert Davis answer about defining a common interface, in the case that you absolutely want to keep the ReSortList function
Seb
Can I have an explanation for -1...?
Seb
Think you, Seb. yes, keep the ReSortList function,because the function requires a recursive
+1  A: 

OK I've just had a look, and I've got 2 points:

  • IList and IEnumerable are not interchangeable - change IList<City> results to IEnumerable<City> results or change your function to take IList<City> instead of IEnumerable<City>.
  • Your Test() method cannot be called the same as the class (Test) so rename it
Andy Shellam
Thinks,The second point I was wrong.
thanks.........
+1  A: 

If you only have ProductCategory and City objects (distinctly unrelated objects), just create two methods. Otherwise give both objects a common interface like IHasParentID so that you can expose the common functionality.

Robert Davis
thanks,perhaps your answer is the best
+3  A: 

Don't make it generic. Your method only takes in a sequence of City and it only appends to a list of City, so why should the method take a sequence of T? There is nothing at all generic about this method; it cannot possibly operate on a sequence of integers or a sequence of strings, so don't make it generic.

Eric Lippert
thanks............