Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When calling <code>Enum.ToObject(...)</code>, the <code>System.Type</code> parameter is supposed to be the type of the enumeration for which you're trying to retrieve an enumeration value.</p> <p>So, you should call it thusly:</p> <pre><code>EnumTest myTestVar = (EnumTest)Enum.ToObject(typeof(EnumTest), i); // i is the integer to be converted to one of the enumeration values. </code></pre> <p>NOTE: This will only work if <code>i</code> is one of the values defined in <code>EnumTest</code>. For example, I don't think the above would work if <code>i = 6</code>, because 6 is not one of the enumeration members, rather, it's a combination of two of its members.</p> <p>HTH.</p> <p><strong>UPDATE:</strong></p> <p>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 <code>Enum</code> 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&mdash;no dice for C#.) So that's out.</p> <p>The other problem I see is with the proposed use of <code>Enum.ToObject(Type t, int i)</code> method. The method summary clearly says "Converts the specified 32-bit signed integer to <strong><em>an</em></strong> enumeration member" <em>[emphasis mine]</em>. So I don't think you could pass in, say <code>6</code>, and expect an enumeration object that has the requisite bit fields set. The documentation at <a href="http://msdn.microsoft.com/en-us/library/ksbe1e7h.aspx" rel="nofollow">MSDN</a> seems to back this up. Also take a look at the documentation for the <a href="http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx" rel="nofollow">Enum.IsDefined</a> method. Finally, <code>System.Enum</code> has a <a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx" rel="nofollow">HasFlag</a> 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 <code>ToObject</code> with "composite" enumeration values.</p> <p>Why don't you just pass in the list of Buttons that you want set to enabled:</p> <pre><code>class test { private List&lt;Button&gt; buttons; public test() { /* Whatever constructor logic you need. */ } public void setButtons(List&lt;Button&gt; btnsToEnable) { btnsToEnable.ForEach(btn =&gt; btn.Enabled = true); // Or perhaps: buttons.Intersect(btnsToEnable).ToList().ForEach(btn =&gt; btn.Enabled = true); } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload