Results 1 to 4 of 4

Thread: Some help Generics

  1. #1

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

    Some help Generics

    Hi,

    Lets say I have a function,which accepts as a parameter a string.

    Depending on the value of this string I would like to return a generic list of different types.

    So, for instance:

    Lets say I pass in the word,"dogs", well then I´d like to return List<Dogs> mydogList = new List<Dogs>();

    If I pass in the word "cars", well the same thing,return List<Cars> myCarlist = new List<Cars>();

    My function I had in mind would be something similiar to:
    (Please note that the code below is incorrect!)

    Code:
    Public List<T> MyFunction(String str)
    {
    switch (str)
    case "Dogs":
    return  List<dogs> dogsList = new list<dogs>();
    Case "Cars":
    return List<cars> carList = new list<cars>();
    }
    But this obviously doesn´t work,...

    Could sb please shine some light on how to improve my approach, or take a totally different one?
    Thanks !

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Some help Generics

    You cannot do what you want by passing a string. You could do it by passing an instance of the type you wanted to make the list items, e.g.
    C# Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     List<int> integers = this.CreateList(0);
    4.  
    5.     List<string> strings = this.CreateList("");
    6. }
    7.  
    8. public List<T> CreateList<T>(T type)
    9. {
    10.     return new List<T>();
    11. }
    If you do it the way you are currently you would have to add a case for every type you wanted to support. In that case your cases should look like this:
    C# Code:
    1. case "String"
    2.     return new List<string>();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #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,...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Some help Generics

    You're trying to do things in a way that they were never intended. You would have to look into reflection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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