Collection of 'constants'

V

VisionSet

[This isn't a web app question - this is just an analogy]
Consider a web application that requires user input from a <select> element.
with struts it is simple & usually logical to iterate through a collection
of options.
Typically we use a simple list of ints say 0 - 9 to represent our choice/s
and pass as parameter value/s.

This is just one example (often this arises not neccessarily in a web app
context), I ask myself is this the best solution to avoid duplication:

String[] choices = {"red","blue","yellow"};
if(choices[myParam].equals("red")) {} // do red stuff

<html>
<select> <!-- obviously this will be produced by iterating over choices -->
<option value="0">red</option>
<option value="1">blue</option>
<option value="2">yellow</option>

often this isn't neccessary, the collection can come naturally from another
class, that already has the ability.

Is this any good? Does anyone deal with this differently?

If iteration wasn't req'd then it would simply become:
public static final int MY_CONST = 5;
and no need for Strings at all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top