Reputation: 2395
I 'm sorry if this is too dumb and simple but I am out of every option I can think of. It is not even real programming. But I have several lines in a switch statement which I 've to use several times. I can't think of any way in which I can store all this code (these lines) and reuse whenever i want.
case 0:
bcolor=Color.rgb(240, 255, 255);//Azure
break;
case 1:
bcolor=Color.BLACK;
break;
case 2:
bcolor=Color.BLUE;
break;
case 3:
bcolor=Color.rgb(165, 42, 42);//Brown
break;
case 4:
bcolor=Color.rgb(255, 127, 50);//Coral
break;
case 5:
bcolor=Color.rgb(00, 255, 255);//cyan
break;
case 6:
bcolor=Color.rgb(255, 00, 255);//Fuchsia
break;
case 7:
bcolor=Color.rgb(255, 215, 00);//Gold
break;
case 8:
bcolor=Color.GRAY;
break;
case 9:
bcolor=Color.GREEN;
break;
case 10:
bcolor=Color.rgb(173, 255, 47);//Green Yellow
break;
case 11:
bcolor=Color.rgb(00, 255, 00);//Lime
break;
case 12:
bcolor=Color.MAGENTA;
break;
case 13:
bcolor=Color.rgb(80, 00, 00);//Maroon
break;
case 14:
bcolor=Color.rgb(80, 80, 00);//Olive
break;
case 15:
bcolor=Color.rgb(255, 165, 00);//Orange
break;
case 16:
bcolor=Color.rgb(255, 192, 203);//pink
break;
case 17:
bcolor=Color.rgb(80, 00, 80);//purple
break;
case 18:
bcolor=Color.RED;
break;
case 19:
bcolor=Color.rgb(244, 196, 30);//Saffron
break;
case 20:
bcolor=Color.rgb(192, 192, 192);//Silver
break;
case 21:
bcolor=Color.rgb(238, 82, 238);//Violet
break;
case 22:
bcolor=Color.WHITE;
break;
case 23:
bcolor=Color.YELLOW;
break;
I have to use all these switch cases in several switch conditions. Isn't is possible to bind them in some kind of label or something. and just call them when I want to use...
Upvotes: 2
Views: 115
Reputation: 36289
One more optimal solution that I have seen and used is to store all of these values in a map (constant time complexity for lookups):
public static Map<Integer, Integer> colorMap = new HashMap<Integer, Integer>();
static
{
colorMap.put(0, Color.rgb(240, 255, 255));
colorMap.put(1, Color.BLACK);
colorMap.put(2, Color.BLUE);
colorMap.put(3, Color.rgb(165, 42, 42));
colorMap.put(4, Color.rgb(255, 127, 50));
colorMap.put(5, Color.rgb(00, 255, 255));
colorMap.put(6, Color.rgb(255, 00, 255));
colorMap.put(7, Color.rgb(255, 215, 00));
colorMap.put(8, Color.GRAY);
colorMap.put(9, Color.GREEN);
colorMap.put(10, Color.rgb(173, 255, 47));
colorMap.put(11, Color.rgb(00, 255, 00));
colorMap.put(12, Color.MAGENTA);
colorMap.put(13, Color.rgb(80, 00, 00));
colorMap.put(14, Color.rgb(80, 80, 00));
colorMap.put(15, Color.rgb(255, 165, 00));
colorMap.put(16, Color.rgb(255, 192, 203));
colorMap.put(17, Color.rgb(80, 00, 80));
colorMap.put(18, Color.RED);
colorMap.put(19, Color.rgb(244, 196, 30));
colorMap.put(20, Color.rgb(192, 192, 192));
colorMap.put(21, Color.rgb(238, 82, 238));
colorMap.put(22, Color.WHITE);
colorMap.put(23, Color.YELLOW);
}
public static int getColor(int lookup)
{
int color = colorMap.get(lookup);
return color == null ? 0 : color;
}
Upvotes: 0
Reputation: 432
Wrap this code in a function and call the function:
public int getColor(int i)
{
switch(i) {
case 0:
return Color.rgb(240, 255, 255);//Azure
case 1:
return Color.BLACK;
case 2:
return Color.BLUE;
case 3:
return Color.rgb(165, 42, 42);//Brown
case 4:
return Color.rgb(255, 127, 50);//Coral
case 5:
return Color.rgb(00, 255, 255);//cyan
case 6:
return Color.rgb(255, 00, 255);//Fuchsia
case 7:
return Color.rgb(255, 215, 00);//Gold
case 8:
return Color.GRAY;
case 9:
return Color.GREEN;
case 10:
return Color.rgb(173, 255, 47);//Green Yellow
case 11:
return Color.rgb(00, 255, 00);//Lime
case 12:
return Color.MAGENTA;
case 13:
return Color.rgb(80, 00, 00);//Maroon
case 14:
return Color.rgb(80, 80, 00);//Olive
case 15:
return Color.rgb(255, 165, 00);//Orange
case 16:
return Color.rgb(255, 192, 203);//pink
case 17:
return Color.rgb(80, 00, 80);//purple
case 18:
return Color.RED;
case 19:
return Color.rgb(244, 196, 30);//Saffron
case 20:
return Color.rgb(192, 192, 192);//Silver
case 21:
return Color.rgb(238, 82, 238);//Violet
case 22:
return Color.WHITE;
case 23:
return Color.YELLOW;
default:
return 0;
}
}
Upvotes: 2
Reputation: 597
As basdwarf said,
Wrap this code in a function and call the function.
You can do this (I didnt take all your cases) :
private void awesomeColorFunction() {
case 0:
bcolor=Color.rgb(240, 255, 255);//Azure
break;
case 1:
bcolor=Color.BLACK;
break;
case 2:
bcolor=Color.BLUE;
break;
}
then call it in your code simply like this :
awesomeColorFunction();
But the smartest way is certainly, as said Pewiity to put your colors in an xml file
Upvotes: 0
Reputation: 72603
Something like this:
public int getColor(int option){
switch(option){
// switch statement in here
}
return bcolor;
}
And now you can call it like this:
int option = 12 // some option
int myColor = getColor(option);
Upvotes: 0
Reputation: 106
First it might be smart to define your colors in an XML file.
So create this file: res/values/colors.xml
And add colors like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="azure">#f0ffff</color>
<color name="brown">#a52a2a</color>
</resources>
So then you can use them as:
R.color.azure
http://developer.android.com/guide/topics/resources/more-resources.html#Color
Upvotes: 2
Reputation: 412
If they're the only cases in the switch statement, you can put them in a static function that returns an integer, and then call it (bcolor = MyClass.getColor(int switchVariable))
Upvotes: 0