Shorten long switch case

后端 未结 2 1785
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 17:08

So let me start by saying I am new to C#. I have a switch statement that currently has 10 different cases, however, I need to use it 3 different times (same 10 cases, different

相关标签:
2条回答
  • 2021-01-28 17:33

    In terms of simplifying a switch statement, I think a Dictionary (if keys were not sequential integers), enumeration, or a List (for something like 1-10 as is the case here) is appropriate, creating a mapped relationship between numbers:

    int[] growth = {0, 60, 80, 90, 40, 120, 130, 50, 70, 40, 150};
    int cropType = 5; // for example
    Console.WriteLine(growth[cropType]); // 120
    

    Here's a dictionary example, which I think is more comprehensible for humans:

    Dictionary<string, int> growth = new Dictionary<string, int>()
    {
        {"Potatoes", 60}, 
        {"Strawberries", 80}, 
        {"Cabbages", 90}, 
        {"Carrots", 40}, 
        {"Melon", 120}, 
        {"Pumpkin", 130}, 
        {"Eggplant", 50}, 
        {"Mushroom", 70}, 
        {"Wheat", 70}, 
        {"Truffle", 150} 
    };
    
    Console.WriteLine(growth["Melon"]);
    

    However, having seen your second switch statement, it appears your unwieldy switches are symptoms of a larger design problem. You may consider adding a Crop class that has member fields for all of the properties you're manipulating, such as type and growth (and any other properties or functions that describe Crop-ness).

    In terms of Global, you may consider a second class that aggregates Crops, such as a Harvest class with a dictionary that keeps tracks of how much of each crop have been harvested.

    Long story short, these design questions can get quite fuzzy and opinion-based, but hopefully this offers some ideas for moving forward.

    0 讨论(0)
  • 2021-01-28 17:34

    Maybe this would be too much, but you can use enums and classes/structs plus a dictionary (as ggorlen suggested)

    Why enums? to avoid using hardcoded numbers; less error-prone and will improve readability;

    private enum CropType
    {
        Undefined = 0,
        Cabbages,
        Carrots,
        Eggplant,
        Melon,
        Mushroom,
        Potatoes,
        Pumpkin,
        Strawberries,
        Truffle,
        Wheat
    }
    
    private struct Crop
    {
        public CropType Type { get; private set; }
        public float GrowthFactor { get; private set; }
        public float HarvestFactor { get; private set; }
    
        public Crop(CropType type, float growthFactor, float harvestFactor) 
        {
            this.Type = type;
            this.GrowthFactor = growthFactor;
            this.HarvestFactor = harvestFactor;
        }
    }
    

    private Dictionary<CropType, Crop> crops;
    private Dictionary<CropType, Crop> Crops 
    {
        get 
        {
            if (crops == null) 
            {
                crops = new Dictionary<CropType, Crop>() 
                {
                    { CropType.Cabbages, new Crop(CropType.Cabbages, 90, 1) },
                    { CropType.Carrots, new Crop(CropType.Carrots, 80, 5) }
                    // here you can add the rest of your products...
                };
            }
            return crops;
        }
    }
    
    public Crop GetCrop(CropType crop) 
    {
        if (!Crops.ContainsKey(type)) 
        {
            Debug.LogWarningFormat("GetCrop; CropType [{0}] not present in dictionary ", type);
            return null;
        }
    
        return Crops[type];
    }
    

    Here is where (finally) you will retrieve the values that you want.

    public float GetGrowthFactor(CropType type) 
    {
        var crop = GetCrop(type);
        return crop == null ? default(float) : crop.GrowthFactor;
    }
    
    public float GetHarvestFactor(CropType type) 
    {
        var crop = GetCrop(type);
        return crop == null ? default(float) : crop.HarvestFactor;
    }
    

    So you will ask for values in this way;

    private void Example()
    {
        var carrotsGrowth = GetGrowthFactor(CropType.Carrots);
    }
    
    0 讨论(0)
提交回复
热议问题