samedi 7 février 2015

C# - Receive string and use it as an enum key

I'm consuming a Currency convertor Web service. The web service namespace contain an enum structure with the currencies:



public enum Currency {
/// <remarks/>
AFA,
/// <remarks/>
ALL,
/// <remarks/>
DZD,
/// <remarks/>
ARS,
/// <remarks/>
AWG,
/// <remarks/>
AUD,
/// <remarks/>
BSD,
/// <remarks/>
BHD
//... AND SO ON
}


The method I use to get the Conversion Rate is (with example):



// ---
// --- Variable type is the enum
// ---
ConvertorReference.Currency from;
ConvertorReference.Currency to;

ConvertorReference.CurrencyConvertorSoapClient service = new ConvertorReference.CurrencyConvertorSoapClient();

// --
// -- Here I asign one of the enum keys
// --
from = ConvertorReference.Currency.USD;
to = ConvertorReference.Currency.COP;


rate = service.ConversionRate(from, to);

Console.Write("1 USD is $" + rate + " Colombia Pesos");
//Outputs: 1 USD is $2380.88 Colombian Pesos


I would like to ask the user for a string, where he can insert for example "USD" or "COP".


After receiving that string I want to know if it is possible to use something like.



curr = Console.ReadLine("Insert the currency you want to perform the operation: ");


Now the variable curr would be something like "USD";


Can I do something like this to get the same result?:



from = ConvertorReference.Currency.curr;




Aucun commentaire:

Enregistrer un commentaire