Saturday, April 01, 2006

String to Enum

I recently needed to use a function whose parameters where Enumerators. And i needed to convert string to Enum for this purpose.Eventually it turned out to be very easy. This is how i did it.

I converted the string to a Enum object using the Enum.Prase() Function.

net.webservicex.www.CurrencyConvertor objCurrencyConvertor =
new net.webservicex.www.CurrencyConvertor();

this._rate = objCurrencyConvertor.ConversionRate((net.webservicex.www.Currency)(Enum.Parse(typeof (net.webservicex.www.Currency),this._fromCurrency)),(net.webservicex.www.Currency)(Enum.Parse(typeof( net.webservicex.www.Currency),this._toCurrency)));

Here "ConversionRate" is the Function and "net.webservicex.www.Currency" is the enumertor that need to passed as the arguement.