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;
        }
    }
wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197
  • 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. – NibblyPig Jun 13 '13 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. – wootscootinboogie Jun 13 '13 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. – Bearcat9425 Jun 13 '13 at 16:39
  • If I understand correctly, you just want to bind the result from the query to a custom enum, right? – AAlferez Jun 13 '13 at 16:52
  • @Ann, yep, that's correct. – wootscootinboogie Jun 13 '13 at 16:53
  • Possible Duplicate question here http://stackoverflow.com/questions/725043/dynamic-enum-in-c-sharp – Bearcat9425 Jun 13 '13 at 17:14
  • Are you looking for a way to product objs that represent strings from a list in a database? I think that you need the following reqs: 1) if the DB gets additional rows added that the app can pick these up without a recompile. 2) you cannot create objs that have strings not in the DB table list. 3) the objs have value-type properties i.e. Two objs with the same string are ==, hash and Equal() each other. I suggest that you make a factory class that reads the DB when new'ed. The instance(string) operator makes class Objs with subtype valuetype or null if the string is not in the list. – Mike Wodarczyk Aug 26 '17 at 20:04
  • 1
    Possible duplicate of [Dynamic enum in C#](https://stackoverflow.com/questions/725043/dynamic-enum-in-c-sharp) – StayOnTarget Jul 03 '18 at 17:45

2 Answers2

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
}
AAlferez
  • 1,480
  • 1
  • 22
  • 48
  • 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. – iCollect.it Ltd Apr 20 '20 at 14:14
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.

Meredith Poor
  • 351
  • 2
  • 9
  • 2
    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) – StingyJack Jun 13 '13 at 17:15