Results 1 to 14 of 14

Thread: Generic value types

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Generic value types

    im trying to make a generic function which converts an enum into a dictionary.

    The dictionary however I want to return would be:

    Dictionary<TValue, string>

    where TValue could be an int, byte, long etc...

    How can I do this?

    this is my current method signature:

    Code:
    public static IDictionary<int, string> ToDictionary<TEnum>() where TEnum : struct

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Generic value types

    Code:
    public static IDictionary<T, string> ToDictionary<T>() where T : struct
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

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

    Re: Generic value types

    I'm not sure that that really helps you because there are a lot of structures that are not enumerations. I'd say that, even with that, you would need to call Type.IsEnum within your method and throw an exception if it's false.
    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

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Generic value types

    Answered on SO:

    http://stackoverflow.com/questions/7...g-t-to-an-enum

    Two main approaches in there: 1) Get as close to the constraint as you can and then throw an exception as jmc suggests
    2) Write it in IL! This then becomes more interesting from a build point of view! (I like this one lots! Although that might be because I won't actually have to deal with it and it's just a theoretical exercise for me! )

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    thats ok in regards to checking if it is an enum and throwing an exception, I have that in place.

    I'm just trying to get the TValue type generic but not sure how. will check the SO link

    in the solution, the enum maybe an int, byte etc... so I want to be able to convert an enum to a dictionary so its <string, TValue> - just not sure how to do the TValue part (not worked with generics much!)
    Last edited by Techno; Mar 13th, 2012 at 02:51 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Generic value types

    That's not how generics work. Sounds like you want something like:

    csharp Code:
    1. public static IDictionary<TUnderlying, string> ToDictionary<TEnum>() where TEnum : Enum<TUnderlying>

    Unfortunately, such a syntax does not exist. Your calling code will be the one to 'close' the generic definition, so it will need to know what the underlying type is before it could specify it (and it needs specifying as you can't infer it from the type of the Enum). Therefore, as it only affects the return value, using generics is a bad fit here, IMO. I would have different methods:

    csharp Code:
    1. public static IDictionary<int, string> ToIntDictionary<TEnum>()...
    2. public static IDictionary<byte, string> ToByteDictionary<TEnum>()...
    3. public static IDictionary<long, string> ToLongDictionary<TEnum>()...

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    Thanks.
    i know what Generics are I guess I was using it in the wrong context than what I meant.

    I was trying to avoid the underlaying type issue and not have to resort to creating x overloads with "strongly/concrete type" for the underlaying type (i.e int, byte, string etc...)

    hmph.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Generic value types

    I know how you feel. I used to think I could avoid some of that with Generics, but when you think about it, in this scenario of trying to get Generic output, what is the difference between this:

    csharp Code:
    1. int integerResult = GetAs<Integer>();
    2. byte byteResult = GetAs<Byte>();
    3. long longResult = GetAs<Long>();

    and this:

    csharp Code:
    1. int integerResult = GetAsInteger();
    2. byte byteResult = GetAsByte();
    3. long longResult = GetAsLong();

    You don't gain anything with Generics - you still have effectively three different methods. What you do get, however, is to make the implementation more complex (if it's even possible).

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    My argument is that the benefit is... less code....

    you create 1 method... and return the result using the developer defined "T" if that makes sense

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: Generic value types

    Looking back at your original question, it appears that there's no actual value to this Dictionary anyway. It appears that you want a Dictionary that will allow you to provide an enum value and give you back a string. Doesn't the ToString method of the enum value already do that? If you want a more descriptive string then you might want to check out CodeBank thread on Friendly Enumeration Names.
    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

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    Thanks.
    actually, thats not quite what I want. What I want is a way to bind to a dropdownlist with a key and value and no dups and that enum datasource could be from any type of enum with any type of underlaying value

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: Generic value types

    Quote Originally Posted by Techno View Post
    Thanks.
    actually, thats not quite what I want. What I want is a way to bind to a dropdownlist with a key and value and no dups and that enum datasource could be from any type of enum with any type of underlaying value
    Then I still don't see that a Dictionary is the way to go.
    csharp Code:
    1. myDropDownList.DataSource = Enum.GetValues(typeof(SomeEnum)).Select(e => new {.Text = e.ToString(), .Value = e});
    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

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    Thanks.

    well thats the short version of what I want. its just to do with a legacy type system so im trying to improve the code from my end to be "better" and more generic/flexible. it may not need to bind to a dropdownlist, could be something else non UI related

    I'd still like the solution with a Dictionary

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Generic value types

    here is the solution:

    Code:
    public static class EnumCollectionHelper
    {
            public static IDictionary<Val, string> ToDictionary<T, Val>()
            {
                Val[] values = Enum.GetValues(typeof(T)) as Val[];
                return values.Zip(Enum.GetNames(typeof(T)), (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v);
            }
    }

    usage:

    Code:
    // get me an enum dictionary with the value being of type byte, as that is what my "EnumHere" is.
    IDictionary myDictionary = EnumCollectionHelper.ToDictionary<EnumHere, byte>();

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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