Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum in constructor - how to?

Tags:

c#

enums

I have a problem with this exercise: Define a class that represent a circle. Constant defined class that holds the value of pi, and a variable defined in readonly holding the color of the circle. The possible colors are defined in enum. Variables defined class to hold the radius of the circle And functions that calculate the perimeter and area of the object. That's what I've done:

    class Circle
{
    public const double PI = 3.14;
    public readonly enum color { Black, Yellow, Blue, Green };
    int radius;
    public Circle(string Color,int radius)
    {
        this.radius = radius;
    }
}

I don't know how can I put the enum selection in the constructor. Thanks for helping.

like image 920
Edward Daker Avatar asked Jun 27 '11 14:06

Edward Daker


1 Answers

public enum Color { Black, Yellow, Blue, Green };

class Circle
{
    public const double PI = 3.14;

    private Color _color;
    int radius;

    public Circle(int radius, Color color)
    {
        this.radius = radius;
        this._color = color;
    }
}

You can also pass string of color, but then you'll have to do Enum.Parse(type of enum, string value).

like image 135
AD.Net Avatar answered Oct 19 '22 22:10

AD.Net