J'ai un groupe de consts, puis je crée dynamiquement une variable qui est égale à l'un des noms consts. Je dois appeler ce const, je sais que je peux utiliser d'autres déclarations, mais je voulais savoir si theres un meilleur moyen Merci!C# appelle un const en utilisant une variable qui correspond au nom const
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;
}
Non, vous ne pouvez pas accéder à une variable de cette façon – Rahul
utiliser un 'Dictionary' à la place –
@LB im tout à fait nouveau pour C# comment pourrais-je utiliser un Dictionnaire pour le faire? –