Brap
Brap

Reputation: 2747

Input enums at run time

I have a method GenerateOutput which prints a list of strings. The method takes multiple enumerations as parameters and outputs a result based on which flags were entered into the method. I know enumerations are designed for compile time, but is it possible to alter the output at runtime, based on what options the user selected in the program? Essentially, I have various check-boxes which represent the possible enumerations. When the user selects an option, that flag should be added as a parameter to the GenerateOutput method. Can this be done? Thanks

Upvotes: 0

Views: 473

Answers (1)

Jackson Pope
Jackson Pope

Reputation: 14640

I think what you want to do (I'm not sure I completely understand your question) is to build up an Enum value at run-time to pass into the function.

Assuming that your enum is specified with the [flags] attribute:

[flags]
public enum TestEnumerations
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    //etc
}

then you could do this:

// In checkbox handlers, e.g.
tEnums |= TestEnumerations.Value1;

// Where you call the method
GenerateOutput(tEnums);

Alternatively, as suggested by Francisco in comments, have a List list (or a HashSet if you only want each enum value to appear once):

// In checkbox handlers, e.g.
list.Add(TestEnumerations.Value1);

// Where you call the method
GenerateOutput(list);

Upvotes: 3

Related Questions