0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user853710
  • 1,715
  • 1
  • 13
  • 27
  • 1
    Why you are passing `Type` when you can pass `EnumTest` as parameter? – HuorSwords May 29 '13 at 17:30
  • Because This is a multi purpose class. It does not work only with EnumTest, But there are also Other Enums with which it has to work. – user853710 May 29 '13 at 17:32
  • then take a look here it's very straight forward http://stackoverflow.com/questions/8447/enum-flags-attribute – MethodMan May 29 '13 at 17:34
  • 1
    Unrelated, but if this is WPF, you should not be creating or manipulating UI elements in procedural code. those disgusting winforms-like type of hacks are really discouraged and WPF doesn't play well with them. – Federico Berasategui May 30 '13 at 16:04

1 Answers1

1

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.

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
  • Because This is a multi purpose class. It does not work only with EnumTest, But there are also Other Enums with which it has to work Unless I can cast it depending on the Type object this is useless. Also sinse the Flags attribute is set to the EnumTest it should be accepting i=6 it with the flags and set the Edit and Delete flags. – user853710 May 29 '13 at 17:42
  • Ah, yes, I guess `ToObject` should do that. I didn't understand your comment about the class needing to use different types. I'll update my answer to reflect this. I may have an idea that will work for you. – fourpastmidnight May 30 '13 at 19:16
  • hope you figure out something. It's supossed to be a really simple way to manage toolbars and activate controls in an general matter. I am actually used to work with python, and I did something like this in Python very easily. C# is much more complicated for advanced stuff. – user853710 May 30 '13 at 19:40
  • Well, python is a dynamic language, while C# is not (strictly speaking). So type-safety is paramount. – fourpastmidnight May 30 '13 at 19:47