1
Answer

Create enum from 2 static arrays?

Jason W

Jason W

10y
1k
1
I'm fairly new to C# and wanted to see if something like this can be done.

I have 2 arrays that are defined like this at the top of my class:

public static char[] ITEMCODE = { 'a', 'b', 'c','d' };
public static string[] ITEMDESC = { "A-Desc", "B-Desc", "C-Desc", "D-Desc" };

What I'd like to do is have it build an enum based on static information during the compile so that the information can be changed there and used elsewhere.  The enum would be based on:

ITEMCODE[x] = ITEMDESC[x]

Is there a way do to that easily? Most of what I could find via googling is creating an enum based on sources that were not set at compile time (such as external XML files) resulting in generating DLLs and such.

My plan is to use Enum.TryParse on the resulting enum and if the char entered is valid, set that value in another array.  I guess the closest thing would be using this like a case statement.

If not easily done that's fine.  I know there's a dozen different ways this could be done, while loops with if statements, for loops with if statements, foreach loops , case statements etc.  I just wanted to see if it could be done this way since I find a lot of things in C# seem to work in general if you can logically think it up. 

Thanks



Answers (1)