Results 1 to 4 of 4

Thread: Some help Generics

Threaded View

  1. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: Some help Generics

    Hi Thanks,

    I eventualy opted for an easier approach sth like:

    Code:
    List<dog> dogs;
    List<car> cars;
    
    private void LoadList(string str)
    {
    switch(str)
    {
    case dog:
                dogs = new List<dog>();
                dogs.add(new dog("jimmy"));
                break;
    case car:
                cars = new List<car>()
                ,....
                ,..
    }
    }
    }
    It would be great though to somehow combine your code with this approach;

    Sth like:

    Code:
    Private List<T> LoadList<T>(T type)
    {
    Problem (1)
    switch(type.GetType())
    {
    case dog.GetType():
          List<T> dogList = new List<T>();
          problem(2)("Would like to add dog items")
          return dogList;
    
    
    case car.GetType():
    break;
    }
    But, run into problems.
    1) First I find I can´t do a Switch based on a variable´s type.
    2) I can only return an empty List<T>. So if the type is "Car", I will only be able to return an empty,List<Car>, I mean by empty, with no Car items.
    I can´t add Car items to the list from within the switch, as I would need Car objects, and I can´t add them to a List<T> type.

    Maybe I am over complicating things, but just would like to learn how to do things in a different way,..
    Thanks!
    Last edited by Rauland; Jun 2nd, 2007 at 01:13 PM. Reason: Code correction,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width