Instead of a simple switch statement

2007/08/31

Tags: C#

This is Luke‘s kind of code. I might be catching the virus

abstract class QIFParserBase {
    public enum LoadOptions {
        All,
        Prices,
        Securities,
        Transactions
    }
    static readonly Dictionary<LoadOptions, Action<QIFParserBase, string[]>> parseFuncs =
                                        new Dictionary<LoadOptions, Action<QIFParserBase, string[]>> {
        {LoadOptions.All, (q,c) => q.ParseAll(c)},
        {LoadOptions.Prices, (q,c) => q.ParsePricesBlocks(c)},
        {LoadOptions.Securities, (q,c) => q.ParseSecurityBlocks(c)},
        {LoadOptions.Transactions, (q,c) => q.ParseTransactionBlocks(c)}
    };
    public QIFParserBase(string fileName, LoadOptions opt) {
        string content = File.ReadAllText(fileName);
        string[] blocks = content.Split(new string[] { "!Type:", "!Option:" },
                                                        StringSplitOptions.RemoveEmptyEntries);
        parseFuncs[opt](this,blocks);
    }
comments powered by Disqus Updated: 2019-01-23 11:41:32 +0100 +0100, Version: 6668e21