Results 1 to 6 of 6

Thread: [RESOLVED] [2.0] Return 2 types with generic parameters?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Resolved [RESOLVED] [2.0] Return 2 types with generic parameters?

    Code:
        public class RoomCollection : List<Room>
        {
            public T IndexOfName<T>(string name)
            {
                if (!(typeof(T) == typeof(int)) && !(typeof(T) == typeof(Room)))
                    throw new Exception("Invalid generic parameter!");
                for (int i = 0; i < this.Count; i++)
                {
                    if (this[i].SiteID == name)
                    {
                        if (typeof(T) == typeof(int))
                            return (T)(object)i;
                        return (T)(object)this[i];
                    }
                }
                if (typeof(T) == typeof(int))
                    return (T)(object)-1;
                return (T)(object)null;
            }
        }
    I want to easily be able to choose what it returns so I made this. How do you properly do this?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [2.0] Return 2 types with generic parameters?

    I have a generic class that returns objects of type T, it is something like

    Code:
    public class RoomCollection<T> 
        {
            public List<T> IndexOfName(string name)
            {
                //codes
                return null;
            }
        }
    Does that make a difference on your current implementation?

    Sampe usage would be

    Code:
    private static RoomCollection<Room> _rooms = null;
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] Return 2 types with generic parameters?

    Quote Originally Posted by dee-u
    I have a generic class that returns objects of type T, it is something like

    Code:
    public class RoomCollection<T> 
        {
            public List<T> IndexOfName(string name)
            {
                //codes
                return null;
            }
        }
    Does that make a difference on your current implementation?

    Sampe usage would be

    Code:
    private static RoomCollection<Room> _rooms = null;
    Yes, I allow the user of the class to choose between 2 returns. if they pass int as the generic parameter it returns the index. If they pass Room it returns the object instead of the index.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] Return 2 types with generic parameters?

    Create two different methods, one that accepts an int and one that accepts a 'Room'. This way, you're not hacking a single method to do two things, but have two methods, each of which does exactly what it says.

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

    Re: [2.0] Return 2 types with generic parameters?

    As mendhak suggests, that method is a complete hack. The name of the method is "IndexOfName", so there's no way that method should be returning an item. It should ONLY be returning an index of an item, which MUST be an int. You should have a completely separate method that returns an item by name.

    Also, if you want to retrieve items by name then you should probably be using a Dictionary, not a List. That's no good if the names aren't unique, of course.
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] Return 2 types with generic parameters?

    I guess you guys are right, thanks.

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