I have a comma delimited file with " as a text qualifier. Currently I have an enum class that hardcodes in the file columns names. However I need to change it to dynamically grab the file column names and put them into the enum. Any suggestions on how to do this in C#?
Asked
Active
Viewed 206 times
1
-
1can you be clearer? showing us the a file sample, and what you want the output to be? – gdoron Apr 20 '12 at 13:49
-
Possible duplicate: http://stackoverflow.com/questions/725043/dynamic-enum-in-c-sharp – Will Apr 20 '12 at 13:51
-
Why do you have to put it in an enum? – scottheckel Apr 20 '12 at 13:52
-
2Enumerations are compile time constructs - what's the point of creating an Enum if you can't use it? – Oded Apr 20 '12 at 13:52
-
To follow up on Oded's comment: an enum allows you to refer to an integer value with a name... at compile time. If you want to do that dynamically (that is, at run time), you can use an in-memory collection, as Servy's answer suggests. – phoog Apr 20 '12 at 13:58
2 Answers
3
An enum is designed to be defined at compile time, and not modified dynamically. While there are ways to do it through reflection, it's not in the least useful because you can't write any code that uses the dynamically generated values.
What you probably want is a Dictionary
where the key is the column name and the value is the index of that column in the file, a List
of column names (so you can find the column name by index), or both (so you can do a lookup in either direction). Dictionary
is the most likely need though, based on your post.

Servy
- 202,030
- 26
- 332
- 449