0

I want to have an Enum that's built from a database query and I'm stuck at how to do it. My thought was to have a method that returns a list of strings and use that to fill the Enum, but I'm unsure if that's possible or the proper way to do it. The table that the Enum will be built from will change very infrequently. What's a good way to get at this?

enum DrugColor
    {

    }
    private List<string> BuildEnum()
    {
        List<string> EnumList = new List<string>();
        using (SqlConnection con = new SqlConnection(cs))
        {
            using (SqlCommand cmd = new SqlCommand("select color from DrugColors", con))
            {
                con.Open();
                cmd.CommandType = CommandType.Text;
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    string colorName;
                    colorName = rdr["Color"].ToString();
                    EnumList.Add(colorName);
                }
            }
            return EnumList;
        }
    }
8
  • Assuming you have created your enum, what would you do with it? I can't picture a use case which suggests you might be going about it the wrong way. Commented Jun 13, 2013 at 16:33
  • I want to use the enum as a way to change the border color of a control on a WebForm. I suppose I'll need to make it of type System.Drawing.Color, but that's trivial, I just want to know how to make an enum from a database in general. Commented Jun 13, 2013 at 16:36
  • I have a project that does something similar, except it produces an assembly which I swap around in some projects. I have some larger tables of lookupTypes and Id's associated, I run this and it exports out an assembly of enums. You need to look into EnumBuilder Class under the System.Reflection.Emit namespace. Commented Jun 13, 2013 at 16:39
  • If I understand correctly, you just want to bind the result from the query to a custom enum, right? Commented Jun 13, 2013 at 16:52
  • 1
    Possible duplicate of Dynamic enum in C# Commented Jul 3, 2018 at 17:45

2 Answers 2

2

So your function should return a list of enums:

private List<DrugColor> BuildEnum()

Thats the first thing. When you declar the list you return, change it to a list of enum too:

List<DrugColor> EnumList = new List<DrugColor>();

When you take the data from the database, each read() is a color so color name is a DrugColor

DrugColor colorName;
colorName = new DrugColor{(DrugColor)Enum.Parse(typeof(DrugColor ),rdr["Color"].ToString()); }

With this you parse the string into the enum.

And then you add it to the list

Voila!

EDIT:

Your enum should contain things:

enum DrugColor
{
  Red,
  Blue,
  Yellow
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does not answer the question asked at all (and I suspect the code does not even do what you suggest). They want to build the enum from the database content, not just parse them from strings.
1

Enums are created at design time. In short, you would not be generating an enum during run time instances, so you're describing a need for code-writing code. How many distinct drug colors are there? Seems like this is done just as easily by hand.

1 Comment

Agree with the design time part, but I code-generate enums from database values all the time. This way the DB and the enum stays in sync and makes for less errors in programs (unless you forget to re-run the codegen)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.