C# – Replacement of enum that requires translation and enumeration

c++enums

We have some stuff that may be exported into various formats. Currently we have these formats represented by an enum like this:

[Flags]
public enum ExportFormat
{
    None = 0x0,
    Csv = 0x1,
    Tsv = 0x2,
    Excel = 0x4,
    All = Excel | Csv | Tsv
}

Problem is that these must be enumerated and they also need a translation or description in the ui. Currently I solved this by creating two extension methods. They work, but I don't really like them or the solution at all… they feel kind of smelly. Problem is I don't really know how I could do this better. Does anyone have any good alternatives? These are the two methods:

    public static IEnumerable<ExportFormat> Formats(this ExportFormat exportFormats)
    {
        foreach (ExportFormat e in Enum.GetValues(typeof (ExportFormat)))
        {
            if (e == ExportFormat.None || e == ExportFormat.All)
                continue;

            if ((exportFormats & e) == e)
                yield return e;
        }
    }

    public static string Describe(this ExportFormat e)
    {
        var r = new List<string>();

        if ((e & ExportFormat.Csv) == ExportFormat.Csv)
            r.Add("Comma Separated Values");

        if ((e & ExportFormat.Tsv) == ExportFormat.Tsv)
            r.Add("Tab Separated Values");

        if ((e & ExportFormat.Excel) == ExportFormat.Excel)
            r.Add("Microsoft Excel 2007");

        return r.Join(", ");
    }

Maybe this is the way to do this, but I have a feeling there must be better ways to do it. How could I refactor this?

Best Solution

You could use the Formats method inside Describe to avoid doing all the bit operations at multiple places, like this:

private static Dictionary<ExportFormat, string> FormatDescriptions =
    new Dictionary<ExportFormat,string>()
{
    { ExportFormat.Csv, "Comma Separated Values" },
    { ExportFormat.Tsv, "Tab Separated Values" },
    { ExportFormat.Excel, "Microsoft Excel 2007" },            
};

public static string Describe(this ExportFormat e)
{
    var formats = e.Formats();
    var descriptions = formats.Select(fmt => FormatDescriptions[fmt]);

    return string.Join(", ", descriptions.ToArray());
}

This way, it is easy to incorporate the string descriptions from an external source or localization, as hinted above.