im trying to pass back from a user control a list of strings that are part of an enum, like this:
<bni:products id="bnProducts" runat="server" ProductsList="First, Second, Third" />
and in the code behid do something like this:
public enum MS
{
First = 1,
Second,
Third
};
private MS[] _ProductList;
public MS[] ProductsList
{
get
{
return _ProductList;
}
set
{
_ProductList = how_to_turn_string_to_enum_list;
}
}
my problem is I dont know how to turn that string into a list of enum, so what should be "how_to_turn_string_to_enum_list"? or do you know of a better way to use enums in user controls? I really want to be able to pass a list that neat
You need to look at the System.Enum.Parse method.
(updated my code because I misread your code)
This is a short solution, but it doesn't cover some very important things like localization and invalid inputs.
Mark your enum with the [Flags] attribute, and combine flags instead of an array of enum values.
Enum.Parse
is the canonical way to parse a string to get an enum:but you'll need to do the string splitting yourself.
However, your property is currently of type
MS[]
- thevalue
variable in the setter won't be a string. I suspect you'll need to make your property a string, and parse it there, storing the results in aMS[]
. For example:I don't know whether you'll need to expose the array itself directly - that depends on what you're trying to do.