Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 303369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:10:56+00:00 2026-05-12T07:10:56+00:00

Thanks to this question I managed to work out how to constrain my generic

  • 0

Thanks to this question I managed to work out how to constrain my generic method to accept only enums.

Now I’m trying to create a generic method so that I can bind a drop-down to any enum I choose, displaying the description in the drop-down, with the value equal to the numeric value of the enum value.

public static object EnumToDataSource<T>() where T : struct, IConvertible {
  if (!typeof(T).IsEnum) // just to be safe
    throw new Exception(string.Format("Type {0} is not an enumeration.", typeof(T)));
  var q = Enum.GetValues(typeof(T)).Cast<T>()
    .Select(x => new { ID = DataUtil.ToByte(x), Description = x.ToString() }) // ToByte() is my own method for safely converting a value without throwing exceptions
    .OrderBy(x => x.Description);
  return q;
}

Looks nice, but ToByte() always returns 0, even if my enumeration has values explicitly set, like so:

public enum TStatus : byte {
  Active = 1,
  Inactive = 0,
}

Outside the generic method, if I cast a value of type TStatus to byte, it works perfectly. Inside the generic method, if I try cast something of type T to byte I get a compiler error.
I can’t find anything in the Enum static interface to do this, either.

So, how do I get the numeric value of the enum inside the generic? (I’ll also accept any other advice about optimizing my code gratefully…)

Edit: Um, er… turns out that the thing wasn’t working… because there was a bug in my ToByte() method… (blush). Oh well, thanks anyway – I learned a lot from this!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:10:56+00:00Added an answer on May 12, 2026 at 7:10 am

    You can do it like this (change DataUtil.ToByte(x) to x.ToByte(null)):

    public static object EnumToDataSource<T>() where T : struct, IConvertible
            {
                if (!typeof (T).IsEnum) throw new Exception(string.Format("Type {0} is not an enumeration.", typeof (T)));
                var q =
                    Enum.GetValues(typeof (T)).Cast<T>().Select(x => new {ID = x.ToByte(null), Description = x.ToString()}).OrderBy(
                        x => x.Description).ToArray();
                return q;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to this question (click me!) , I have the Source property of my
I understand the problem that OSGI solved thanks to this question.... What does OSGi
Thanks for looking at this question. I wanted to know how can I use
and thanks for looking at this question. I am porting a Linux (2.6.37) driver
Thanks in Advance for reading and answer this question. I got button in asp
This question is related to another question of mine. Thanks to some help I
This is a follow-up question to this question I asked earlier. Btw thanks Neil
i've been asked this question and id not know the answer. Thanks for any
Thanks to the answer on this stackoverflow question I was able to get the
Hi and thanks for looking at this newbi question. I have looked for anykind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.