c# - Correct way to initialize Enums inside a class constructor -


i looking @ enums in wrong way want make sure have right theory in how use them.

say have enum called colour.

enum colour { red, green, blue }; 

red green , blue represented there 0-255 values. i'm trying initialize enum inside class shape , i'm not sure how go it.

public class shape {      colour colour;      public shape(colour c)     {          //some attempts @ initialization.            //treating object          this.colour =              c{                 255,255,255             };           //again          this.colour.red = c.red          this.colour.blue = c.blue          this.colour.green = c.green           colour.red = c.red?            }     } } 

i'm way off in terms of how i'm thinking enums. can give me pointers?

in case, might want colour struct instead of enum. in c#, enums single-valued constructs, have 3 values (red, green, , blue). here's might instead:

public struct colour  {     private byte red;     private byte green;     private byte blue;      public colour(byte r, byte g, byte b)      {         this.red = r;         this.green = g;         this.blue = b;     } }  public class shape {     public colour colour { get; private set; }      public shape(colour c)     {         this.colour = c;     } } 

and when you're creating shape objects:

var shape = new shape(new colour(203, 211, 48)); 

edit: chris pointed out in comments, use system.drawing.color struct provided framework. example above simplified to:

using system.drawing;  public class shape {     public color colour { get; private set; }      public shape(color c)     {         this.colour = c;     } }  var shape = new shape(color.fromargb(203, 211, 48)); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -