I could be looking at Enums in the wrong way but want to make sure I have the right theory in how to use them.
Say we have an enum called Colour.
enum Colour { Red, Green, Blue };
Red Green and Blue are represented by there 0-255 values.
I’m trying to initialize this enum inside a class shape and I’m not really sure how to go about it.
public class Shape
{
Colour colour;
public Shape(Colour c)
{
//Some attempts at initialization.
//Treating It like an 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 probably way off in terms of how I’m thinking about enums. Can anyone give me some pointers?
In this case, you might want Colour to be
structinstead of an enum. In C#, enums are single-valued constructs, but you have three values (red, green, and blue). Here’s what I might do instead:And then when you’re creating your shape objects:
EDIT: As Chris pointed out in the comments, you could simply use the
System.Drawing.Colorstruct provided by the framework. The example above would be simplified to: