1

This is what I'm using to select function based on enum type. Would there be an approach where I didn't have the switching CalcMe function?

namespace ClassLibrary1
{
public class Playbox
{
    //types:
    //0 - red hair
    //1 - blue hair

    //defines function to input based on hairtype.
    //red:
    // input*10
    //blue:
    // input*12

    public enum Phenotypes
    {
        red,
        blue
    }

    static public int Red(int input)
    {
        return input*10;
    }

    static public int Blue(int input)
    {
        return input*12;
    }

    static public int CalcMe(Phenotypes phenotype, int input)
    {
        switch (phenotype)
        {
            case Phenotypes.red:
                return Red(input);
            case Phenotypes.blue:
                return Blue(input);
            default:
                return 0;
        }
    }

    public class MyObject
    {
        int something;
        Phenotypes hairtype;

        public MyObject()
        {
            Random randy = new Random();
            this.hairtype = (Phenotypes)randy.Next(2); //random phenotype
            this.something = CalcMe(hairtype, randy.Next(15)); //random something
        }
    }
}
}
UpTide
  • 307
  • 3
  • 13
  • 1
    You could use a `Dictionary` that maps the enum to methods (probably a `delegate` such as [`Func`](https://msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx)) – UnholySheep Jul 17 '17 at 22:45
  • @UnholySheep I'm looking at that and it seems that I would just be replacing my `Switch` with the `Dictionary` – UpTide Jul 17 '17 at 22:53
  • Keeping in mind, a `switch` is a reasonable expressive way to describe this type of lookup, and performance-wise, the compiler will transform the code into a dictionary-based lookup anyway, if you have enough `case` statements to make it a worthwhile implementation. – Peter Duniho Jul 17 '17 at 22:54
  • @PeterDuniho this is a duplicate of 7355843 this should be closed. – UpTide Jul 17 '17 at 22:55
  • You should be able to vote to close your own question as a duplicate, I think. Have you tried? – Peter Duniho Jul 17 '17 at 22:57

3 Answers3

6

You can use a dictionary like this

Dictionary<Phenotypes, Func<int, int>> Mappings = new Dictionary<Phenotypes, Func<int, int>>()
{
    {Phenotypes.red, x=> Red(x) },
    {Phenotypes.blue, x=> Blue(x) }
};

Now you can call it like

var something = Mappings[Phenotypes.blue](666);
EZI
  • 15,209
  • 2
  • 27
  • 33
0

Given:

//types:
//0 - red hair
//1 - blue hair

You could do this:

static public int CalcMe(Phenotypes phenotype, int input)
{
    return input * 10 + 2* (int)phenotype;
}
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • My problem with that is what if I need to change red to multiply by 7 but blue to divide by 20? This seems difficult to change. – UpTide Jul 17 '17 at 22:51
  • If you are going to need to switch your logic in the future then a switch is the way to go unless you use classes which you can call a single function. – Alexander Higgins Jul 17 '17 at 23:14
0

If it is just value multiplication you need just assign integer values to those enum types

public enum Phenotypes
{
    red = 10,
    blue = 12
}

then all you need to do is use their integer values

Phenotypes[] phenoTypeArray = new Phenotypes[]{ red, blue};

public MyObject()
{
    Random randy = new Random();
    this.hairtype = phenoTypeArray[randy.Next(2)]; //random phenotype
    this.something = (int)hairtype * randy.Next(15);
}
Deniz Gokce
  • 71
  • 1
  • 6