Type casting inside generic class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Segurini

    #1

    Type casting inside generic class

    Hi,

    I am converting a regular class

    enum ENUM_TEST
    {
    test1,
    test2,
    test3,
    }

    class enum_no_generic
    {
    public static bool IsValid(int id)
    {
    return System.Enum.IsD efined(typeof(E NUM_TEST), (ENUM_TEST)id);
    }
    }

    in a generic class

    class enum_t<T>
    {
    public static bool IsValid(int id)
    {
    return System.Enum.IsD efined(typeof(T ), (T)id); // line 36
    }
    }

    but I get the following build error:

    C:\...\Program. cs(36,50): error CS0030: Cannot convert type 'int' to 'T'

    that means that the conversion "(T)id" is not permitted.

    Is there any way to achieve my goal?

    TIA

    Marco.
  • Stoitcho Goutsev \(100\)

    #2
    Re: Type casting inside generic class

    Marco,

    You don't need that conversion. Enum.IsDefined accepts Object, so you don't
    need to cast to anything.

    return System.Enum.IsD efined(typeof(T ), id);



    --
    HTH
    Stoitcho Goutsev (100)

    "Marco Segurini" <marcosegurini@ virgilio.it> wrote in message
    news:efbGRcUOGH A.3272@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi,
    >
    > I am converting a regular class
    >
    > enum ENUM_TEST
    > {
    > test1,
    > test2,
    > test3,
    > }
    >
    > class enum_no_generic
    > {
    > public static bool IsValid(int id)
    > {
    > return System.Enum.IsD efined(typeof(E NUM_TEST), (ENUM_TEST)id);
    > }
    > }
    >
    > in a generic class
    >
    > class enum_t<T>
    > {
    > public static bool IsValid(int id)
    > {
    > return System.Enum.IsD efined(typeof(T ), (T)id); // line 36
    > }
    > }
    >
    > but I get the following build error:
    >
    > C:\...\Program. cs(36,50): error CS0030: Cannot convert type 'int' to 'T'
    >
    > that means that the conversion "(T)id" is not permitted.
    >
    > Is there any way to achieve my goal?
    >
    > TIA
    >
    > Marco.[/color]


    Comment

    Working...