6

First of all, I want to say that I'm new to C#, so this question may seem completely off track.

I have a set of enumerables called ShapeType:

Cube, Sphere, Rectangle, Ellipse

And a method to return a random value from the enumerables:

private static ShapeType GetRandomShape()
{
    Array values = Enum.GetValues(typeof(ShapeType));
    Random random = new Random();
    ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
    return randomShape;
}

Every enumerable has a corresponding concrete class. And the question I'm wondering about is if you can instantiate a class by using the random enumerable value randomShape, kind of like this:

private static Shape GetRandomShape()
{
    Array values = Enum.GetValues(typeof(ShapeType));
    Random random = new Random();
    ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
    Shape shape = new randomShape(); // *Here use the randomShape-variable as type*
    return shape;
}

Is this possible or is it just wishful thinking?

1
  • 1
    "Every enumerable has a corresponding concrete class" - you can use dictionary to map enum value to corresponding type/instance. Another option is not to use enum at all, rather implicit to int conversion (where enum value is used now).
    – Sinatr
    May 4, 2017 at 12:33

4 Answers 4

3

You can use a dictionary to retrieve a factory function for every value of the enum:

static readonly Dictionary<ShapeType, Func<Shape>> _factoryLookup = new Dictionary<ShapeType, Func<Shape>>
{
    [ShapeType.Cube] = () => new Cube(),
    [ShapeType.Ellipse] = () => new Ellipse(),
    [ShapeType.Rectangle] = () => new Rectangle(),
    [ShapeType.Sphere] = () => new Sphere(),
};

static readonly Random random = new Random();

private static Shape GetRandomShape()
{
    Array values = Enum.GetValues(typeof(ShapeType));
    ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
    Func<Shape> factory = _factoryLookup[randomShape];
    Shape shape = factory();
    return shape;
}
2

You need to use factory method pattern:

public class Shape {}

public class Cube : Shape {}

public class Sphere : Shape {}

public class Rectangle : Shape {}

public class Ellipse : Shape {}

public Shape randomShape(ShapeType shapeType)
{
    switch(shapeType)
    {
         case ShapeType.Cube:
         return new Cube();
         ...
    }
}
1
  • 1
    I don't think that's the question.
    – Natrium
    May 4, 2017 at 12:38
1

Create a Dictionary, with enumvalue as key, and type as value.

Dictionary<ShapeType, Type> dic = new Dictionary<ShapeType, Type>();
dic.Add(ShapeType.Cube, typeof(Cube));

// ...

private static Shape GetRandomShape()
{
    Array values = Enum.GetValues(typeof(ShapeType));
    Random random = new Random();
    ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
    Shape shape = Activator.CreateInstance(dic[randomShape]); // *Here use the randomShape-variable as type*
    return shape;
}
0

Instead of

enum ShapeType { Cube, Sphere, Rectangle, Ellipse }

You can use something like

abstract class Figure
{
    protected int id;

    public static implicit operator int(Figure figure) => figure.id;
    public static implicit operator Figure(int value) => Figures().First(figure => figure.id == value);

    protected Figure(int id)
    {
        this.id = id;
    }

    public static Cube Cube { get; } = new Cube(1);
    public static Sphere Sphere { get; } = new Sphere(2);

    public static IEnumerable<Figure> Figures()
    {
        yield return Cube;
        yield return Sphere;
    }

    public override string ToString() => this.GetType().ToString();
}

class Cube : Figure
{
    public Cube(int id) : base(id) { }
}
class Sphere : Figure
{
    public Sphere(int id) : base(id) { }
}

this will let you to do something like:

Console.WriteLine((object)(Figure)1); // Cube
Console.WriteLine((object)(Figure)2); // Sphere

I am using such approach in many places instead of enum, which is often not sufficient (as it needs to hold extra information than just name and int).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.