Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As posted elsewhere, the answer to your exact question is, "No." And my addition: "Use a more expressive language, like Lisp, Python, Ruby, or one like them."</p> <p>I suspect that you're asking the wrong question though. What you really want is a way to get the alpha value of an arbitrary color. The object-oriented design advocate would use this as an opportunity to fill your head with factory methods, subtype polymorphism, enumerated types, and other such things.</p> <p>Given my completely-uneducated guesstimate about your level of coding and the complexity of the project, it looks like an acceptable workaround (if not The Right Way) is a lot simpler:</p> <pre><code>int newAlpha = 0; if(colorName.equals("yellow") newAlpha = yellow.getAlpha(); else if(colorName.equals("red") newAlpha = red.getAlpha(); else if(colorName.equals("blue") newAlpha = blue.getAlpha(); else //raise some kind of exception, assert false, or do something else that seems sensible </code></pre> <p>(Note that I've slightly changed a few names to match Java's style.)</p> <p>I have no idea what's going on in the <code>Color</code> class (or even how <code>red</code>, <code>yellow</code>, and <code>blue</code> are different--with the current code they'll be the same thing (local variable names do not affect the object they refer to)), but it's probably appropriate to break the if-sequence into a separate method. And/or use a <code>HashMap</code> thus:</p> <pre><code>HashMap&lt;String, Integer&gt; alphaValues = new HashMap&lt;String, Integer&gt;(); alphaValues.put("red", /* something like red.getAlpha() */); //etc. // ... newAlpha = alphaValues.get(colorName); </code></pre> <p>I haven't really addressed the scope issue--I'm sure you can figure it out.</p> <p>Hope that helps!</p> <p><em>Edit: Dave Newton beat me to the punch about the hashmap--he edited his post while I was creating mine.</em></p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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