Enum in the constructor - how?

I have a problem with this exercise: Define a class representing the circle. A constant defined class that contains the value of pi, and a variable defined when reading only the color of the circle. Possible colors are listed. Variables define a specific class to hold the radius of a circle AND functions that calculate the perimeter and area of โ€‹โ€‹an object. This is what I did:

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 do not know how I can put an enumeration selection in a constructor. Thanks for the help.

+6
source share
8 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 a color string, but then you will need to do Enum.Parse (enumeration type, string value).

+9
source

Just define your Enum outside the class definition and declare a local instance of the read-only type.

 enum Color { Black, Yellow, Blue, Green }; class Circle { public const double PI = 3.14; public readonly Color color; int radius; public Circle(string colorValue, int r) { color = ( Color ) Enum.Parse( typeof( Color ), colorValue ); radius = r; } } 
+3
source

Try the following:

 public enum ColorEnum { Black, Yellow, Blue, Green } public class Circle { public const double PI = System.Math.PI; public ColorEnum Color; public Circle(ColorEnum color,int radius) { this.radius = radius; this.Color = color } } 
0
source

Enum is a type. When you declare Enum, you actually define a new type inside your program. Therefore, if you want to pass the value of Enum as a parameter, you need to declare a parameter of this type.

0
source

You need to declare enum and then use it as a variable type.

 public enum Color { Black, Yellow, Blue, Green }; public readonly Color myColor; 
0
source

try this code

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

for use

  Circle circle = new Circle(Color.Blue,100); 
0
source

Use your personal field and only expose the recipient. I will also make enum a public class and pass it directly:

  class Circle { public const double PI = 3.14; private Color _color; int radius; public Circle(Color Color,int radius) { _color = Color; this.radius = radius; } public Color Color { return _color; } } public enum Color { Black, Yellow, Blue, Green } 
0
source

how about below

 public enum Color { Black, Yellow, Blue, Green } class Circle { public const double PI = 3.14; public Color Color { get; private set; } int radius; public Circle( int radius,Color color) { this.radius = radius; this.Color = color; } } 
0
source

Source: https://habr.com/ru/post/891456/


All Articles