Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting correct Enum
    text
    copied!<p>I have a number of Enums each of which contain the names of attributes to be tested. The problem I have is how to select the relevant enum for the object. How can I define just a Enum variable which use throughout my code which can be set through an initalise method.</p> <p>EDIT:</p> <p>Sorry for the delayed reponse. I had to step away from the desk</p> <p>It very well be bad design. I have a few enums as follows:</p> <pre><code>public enum AccountGrpEnum { Account("Account"), AccountType("AccountType"), AcctIDSource("AcctIDSource"); private static Set&lt;String&gt; grpNames = new HashSet&lt;String&gt;(3) {{ for(AccountGrpEnum e : AccountGrpEnum.values()) { add(e.toString()); } }}; public static boolean contains(String name) { return grpNames.contains(name); } private String name; private AccountGrpEnum(String name) { this.name = name; } public String toString() { return this.name; } } </code></pre> <p>Another <code>Enum</code>:</p> <pre><code>public enum BlockManValEnum { avgPx("avgPx"), quantity("quantity"), securityIDSource("securityIDSource"), securityID("securityID"), blockStatus("blockStatus"), side("side"); private static Set&lt;String&gt; names = new HashSet&lt;String&gt;(9) {{ for(BlockManValEnum e : BlockManValEnum.values()) { add(e.toString()); } }}; public static boolean contains(String name) { return names.contains(name); } private String name; private BlockManValEnum(String name) { this.name = name; } public String toString() { return this.name; } } </code></pre> <p>Within my code I am checking the fields of an incoming object to see they are contained within the Enum. As follows:</p> <pre><code>if (BlockManValEnum.contains(fields[i].getName())) </code></pre> <p>however I would like it to be along the lines of </p> <pre><code>if (variableEnum.contains(fields[i].getName())) </code></pre> <p>Where <code>variableEnum</code> can be set at runtime.</p> <p>Hope this is clearer guys</p>
 

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