Reputation: 1767
I have a small problem. This is a simplified version of my problem.
I have a class that has a list of buttons with its names (New,Edit,Delete) as well a object of type "Type"
class test
{
private List<Button> buttons; //
private Type type;
public test(Type t)
{
this.type = t;
/* some code to create the buttons from the enum.
got this already covered but "Restore" does not get created by intention*/
}
public void setButtons(int i)
{
}
}
the type is an enum with the flags attribute
[Flags] enum EnumTest{None=0, New=1, Edit=2, Delete=4, Restore=8}
What I would like to do is to have the "setButtons" method to create an instance of "type", set the value from "int i" to the flags values, go through the list of buttons and set the "Enabled" property of the button depending on if the flag is raised, where the buttons "Name" property is depending on the Flags Name in the Enum (EnumTest or any other)
I tried several stuff like
var enu = Enum.ToObject(type, i);
But I cannot set the flags, or I don't know how. I was thinking of using reflection while creating it. seems like the "ToObject" method does not play well with the flags attribute. "TryParse" seems to do, but I have no Idea how to make it load dynamically from "Type".
Please, take into consideration that this is a highly simplified version of my problem, but where it would keep the meaning.
Upvotes: 0
Views: 2309
Reputation: 4234
When calling Enum.ToObject(...)
, the System.Type
parameter is supposed to be the type of the enumeration for which you're trying to retrieve an enumeration value.
So, you should call it thusly:
EnumTest myTestVar = (EnumTest)Enum.ToObject(typeof(EnumTest), i); // i is the integer to be converted to one of the enumeration values.
NOTE: This will only work if i
is one of the values defined in EnumTest
. For example, I don't think the above would work if i = 6
, because 6 is not one of the enumeration members, rather, it's a combination of two of its members.
HTH.
UPDATE:
Well, there are two things that I see here. I thought maybe you could use generics to get around your problem, but you aren't allowed to use Enum
as a type constraint on the generic type parameter. (This is where I like Java enumerations better than C# enumerations. You can actually define your own custom methods in your Java enumerations—no dice for C#.) So that's out.
The other problem I see is with the proposed use of Enum.ToObject(Type t, int i)
method. The method summary clearly says "Converts the specified 32-bit signed integer to an enumeration member" [emphasis mine]. So I don't think you could pass in, say 6
, and expect an enumeration object that has the requisite bit fields set. The documentation at MSDN seems to back this up. Also take a look at the documentation for the Enum.IsDefined method. Finally, System.Enum
has a HasFlag method that may be useful for you, too. I think that this may be the root of your problem. I've never tried to use ToObject
with "composite" enumeration values.
Why don't you just pass in the list of Buttons that you want set to enabled:
class test
{
private List<Button> buttons;
public test() { /* Whatever constructor logic you need. */ }
public void setButtons(List<Button> btnsToEnable)
{
btnsToEnable.ForEach(btn => btn.Enabled = true);
// Or perhaps:
buttons.Intersect(btnsToEnable).ToList().ForEach(btn => btn.Enabled = true);
}
}
There are multiple ways of using LINQ to accomplish what you want. It really depends on your object model. Hopefully these thoughts will get you started down another path that works for your scenario.
Upvotes: 1