This discussion is archived
2 Replies Latest reply: Jan 6, 2008 12:12 PM by 807601 RSS

[Enums] How to? Method must return a result of type Enum

807601 Newbie
Currently Being Moderated
Hello,
I have a little problem. I write a program as below:
 
public enum MyColor {
          BLACK (0,0,0),
          WHITE (255,255,255),
          RED (255,0,0),
          GREEN (0,255,0),
          BLUE (0,0,255),
          YELLOW (255,255,0),
          CYAN(0,255,255),
          MAGENTA (255,0,255);
          
          int r, g, b;
          
          MyColor(int rc, int gc, int bc) {
               r=rc; g=gc; b=bc;
          }
          
          boolean compare(int rc, int gc, int bc) {
               if(this.r==rc && this.g==gc && this.b==bc)
                    return true;
               else return false;
          }
          
          public static MyColor giveName(int rc, int gc, int bc) {
               for(MyColor tmp: MyColor.values()) {
                    if(rc==tmp.r && gc==tmp.g && bc==tmp.b) return tmp;
               }
          }
}
and have a question: what is wrong with method giveName(). It must return a color(RED, BLACK, ...) which has the same RGB scheme as integer values of parameters (rc, gc, bc) or return null. How to return this values?
Thanks for all, A.