Tom Rouse
Tom Rouse

Reputation: 61

C# call a const using a variable that matches the const name

I have a group of consts, then I dynamically create a variable that is equal to one of the consts name. I need to call that const, I know I can use if else statements but I wanted to know if theres a better way Thanks!

    public const int LifeBand1Standard = 78;
    public const int LifeBand1Multi = 61;
    public const int LifeBand2Standard = 71;
    public const int LifeBand2Multi = 56;
    public const int LifeBand3Standard = 62;
    public const int LifeBand3Multi = 48;
    public const int LifeBand4Standard = 56;
    public const int LifeBand4Multi = 44;
    public const int LifeBand5Standard = 45;
    public const int LifeBand5Multi = 35;

    // Band discounts/loads for trauma cover
    public const int TraumaBand1Standard = 140;
    public const int TraumaBand1Multi = 126;
    public const int TraumaBand2Standard = 135;
    public const int TraumaBand2Multi = 121;
    public const int TraumaBand3Standard = 121;
    public const int TraumaBand3Multi = 109;
    public const int TraumaBand4Standard = 110;
    public const int TraumaBand4Multi = 99;
    public const int TraumaBand5Standard = 100;
    public const int TraumaBand5Multi = 90;

    protected float GetPercentageFromBand(int band)
    {
        var constantName = "Life";

        if (IsTraumaCover == true)
        {
            constantName = "Trauma";
        }

        constantName += "Band" + band;

        if (IsMultiLife == true)
        {
            constantName += "Multi";
        }
        else
        {
            constantName += "Standard";
        }

        // Constant should look something like LifeBand3Standard and will return the value of the const LifeBand3Standard
        BandPercentage = InsuranceBandFetcher.constantName;

    }

Upvotes: 1

Views: 918

Answers (3)

Rahul
Rahul

Reputation: 77866

Use a enum coupled with a Dictionary something like below (just a sample code)

public enum MyConstant
{
    LifeBand1Standard = 78,
    LifeBand1Multi = 61,
    LifeBand2Standard = 71,
    LifeBand2Multi = 56,
    LifeBand3Standard = 62,
    LifeBand3Multi = 48
}

Keep the data in dictionary

        Dictionary<string, MyConstant> dict = new Dictionary<string, MyConstant>
        {
            { "TraumaCover ", MyConstant.LifeBand3Multi},
            { "MultiLife ", MyConstant.LifeBand2Standard}
        };

        Console.WriteLine(dict["MultiLife "]);

Per your comment, another sample code. You can use Enum.Parse() for that purpose like

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string key = PrepareConstant(1, false);
            MyConstant val = (MyConstant)Enum.Parse(typeof(MyConstant), key, true);

            Dictionary<string, MyConstant> dict = new Dictionary<string, MyConstant>
            {
                { key, val},
            };

            Console.WriteLine(dict[key]); // output: LifeBand1Standard

            Console.ReadKey();
        }

        static string PrepareConstant(int band, bool isMultiLife)
        {
            var constantName = "Life";
            constantName += "Band" + band;
            constantName += (isMultiLife) ?  "Multi" : "Standard";

            return constantName;
        }
    }

    public enum MyConstant
    {
        LifeBand1Standard = 78,
        LifeBand1Multi = 61,
        LifeBand2Standard = 71,
        LifeBand3Standard = 62,
        TrumaBand2Multi = 56,
        TrumaBand3Multi = 48
    }
}

Upvotes: 2

MarkPflug
MarkPflug

Reputation: 29508

What you are trying to do is not possible without reflection. Even with reflection, this is not a recommended design.

What you appear to be wanting is a function that maps from three values: 1. multi or standard (boolean or enum). 2. life or trauma (boolean or enum). 3. Number (int) (or byte, or custom struct) and returns a number of some sort (a percentage?).

enum BandKind {
 Multi,
 Standard,
}

enum BandType {
 Life,
 Trauma
}

int GetBandValue(BandKind kind, BandType type, int num) {

/// impl here
}

Okay, so how to implement GetBandValue? You can use nested switch statements and return literal values, that would be one option, but nested switches end up getting kinda ugly. You could create a multidimentional array and index into it using the values.

int[][][] BandData = {{{78, 61},{71, 56},...}}}; // build this array with your real values.

Then your implementation can be:

int GetBandValue(BandKind kind, BandType type, int num) {

    return BandData[(int)kind][(int)type][num];
}

Upvotes: 0

decPL
decPL

Reputation: 5402

Use a dictionary, instead of magic strings (especially ones created programmatically).

I don't understand your business domain, so my enum names/values might be completely weird, but here's how I would approach the problem:

public enum Cover
{
    Life,
    Trauma
}

public enum MultiLife
{
    Multi,
    Standard
}

public readonly Dictionary<Tuple<Cover, int, MultiLife>, int> Values
 = new Dictionary<System.Tuple<UserQuery.Cover, int, UserQuery.MultiLife>, int>
 {
    [Tuple.Create(Cover.Life, 1, MultiLife.Standard)] = 78,
    [Tuple.Create(Cover.Life, 1, MultiLife.Multi)] = 61,
    [Tuple.Create(Cover.Life, 2, MultiLife.Standard)] = 71,
    // [...]
 };

Then usage would be:

var bandPercentage
 = Values[Tuple.Create(IsTraumaCover ? Cover.Trauma : Cover.Life,
                       band,
                       IsMultiLife ? MultiLife.Multi : MultiLife.Standard)];

Upvotes: 0

Related Questions