dynamic enum creation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Given this definition of an enum:

enum MyEnum {
[Description("This is a descripiton of D1")]
d1=1,
[Description("This is a description of D2")]
d2=2
}

Is there a way to dynamically create this same enum at runtime? I want to
read variable data from a file, but this data into an enum to allow the user
to make a choice from a CheckedListBox. I am able to do this from a
predefined enum definition but want to be able to do it with variable data
read from files. As the mouse moves over the enumeration the "Description"
is shown to the user.

Is there some example somewhere of how this can be done?
 
Given this definition of an enum:

enum MyEnum {
[Description("This is a descripiton of D1")]
d1=1,
[Description("This is a description of D2")]
d2=2
}

Is there a way to dynamically create this same enum at runtime? I
want to read variable data from a file, but this data into an enum to
allow the user to make a choice from a CheckedListBox. I am able to
do this from a predefined enum definition but want to be able to do it
with variable data read from files. As the mouse moves over the
enumeration the "Description" is shown to the user.

Is there some example somewhere of how this can be done?


Are you specifically tied to dynamically creating an enum? Because you
don't have to add enum's to a CheckedListBox - you can add any object that
overrides ToString and it will display whatever the output of ToString is.

So instead of going to the pain of dynamically creating an enum, just
create a class that overrides ToString and create one for each value you
read from the database.

It doesn't answer your question, but it seems to address the issue you are
trying to solve.

-mdb
 
Steve Teeples said:
Given this definition of an enum:

enum MyEnum {
[Description("This is a descripiton of D1")]
d1=1,
[Description("This is a description of D2")]
d2=2
}

Is there a way to dynamically create this same enum at runtime? I want to
read variable data from a file, but this data into an enum to allow the
user
to make a choice from a CheckedListBox. I am able to do this from a
predefined enum definition but want to be able to do it with variable data
read from files. As the mouse moves over the enumeration the
"Description"
is shown to the user.

Is there some example somewhere of how this can be done?

I think what you are looking for in this context is not an enum
(programmatic name) but instead a list of dynamic values read from a
database (text file, spreadsheet, dbms, etc.). What you could do to get
this to work would be to load your data into a DataTable and then bind this
DataTable to the CheckedListBox. I'm not sure without testing, but I
believe this to be an easier route than dynamically creating an enum.

HTH,
Mythran
 
Hi ,

Frankly there is no use at all for a "dynamic enum" , an enum is just a nice
way to give names to values, it's intended only at easying the written of
code and is interpreted at compile time.

What you are after is a collection
 
No use for a Dynamic enum?

How would you suggest creating a list to be displayed in a property grid, built with values only exposed at runtime?

A collection will not give you a list of values, but a new property grid view with each collection item as an object.
 
Back
Top