views:

387

answers:

2

Is there any good way to convert strings like "xlSum", "xlAverage", and "xlCount" into the value they have under Microsoft.Office.Interop.Excel.XlConsolidationFunction?

I guess reflection would be slow (if its possible). There are about 10 of these constant values. I was trying to avoid a large switch statement if possible.

A: 

Instead of switch you can always use a Dictionary<string, ...> and fill it once when the application starts

Dror Helper
+5  A: 

This is an enum so you should be able to use

using Microsoft.Office.Interop.Excel;

XlConslidationFunction func = (XlConsolidationFunction)
                               Enum.Parse( typeof(XlConsolidationFunction),
                                           stringVal );
tvanfosson
You'll need a cast between "=" and "Enum", but +1
Marc Gravell
(as won't work for an enum)
Marc Gravell
thx. I never use the as syntax but I was trying to make it prettier for the post.
tvanfosson
Enum.Parse() - guess I never stumbled upon this before. Nice.
tyndall