> if (String.Equals(mkvp.Key, "PRI_KEY", StringComparison.OrdinalIgnoreCase)) > groupMember.PrimaryKey = Convert.ToInt32(mkvp.Value); > > if (String.Equals(mkvp.Key, "FIRST", StringComparison.OrdinalIgnoreCase)) > groupMember.FirstName = mkvp.Value; > > if (String.Equals(mkvp.Key, "LAST", StringComparison.OrdinalIgnoreCase)) > groupMember.LastName = mkvp.Value; > > if (String.Equals(mkvp.Key, "GstNo", StringComparison.OrdinalIgnoreCase)) > groupMember.GuestNo = Convert.ToInt64(mkvp.Value); > > if (String.Equals(mkvp.Key, "Notes", StringComparison.OrdinalIgnoreCase)) > groupMember.Notes = mkvp.Value;And - come to think of it
class Test_DoCase { internal static void Go() { GroupMember groupMember = new GroupMember(1, "Gregory", "Adam", 234, "none"); MKVP mkvp = new MKVP("FIRST", "Alan"); DoCase.Execute( new Case( () => mkvp.Key == "FIRST", delegate { groupMember.FirstName = mkvp.Value; } ) , new Case( () => mkvp.Key == "LAST", delegate { groupMember.LastName = mkvp.Value; } ) // otherwise or default case , new Case(delegate { throw new Exception("No otherwise/default");}) ); Console.WriteLine("{0} {1} ", groupMember.FirstName, groupMember.LastName); } } class MKVP { public string Key; public string Value; public MKVP(string key, string value) { Key = key; Value = value; } } class GroupMember { public int PrimaryKey; public string FirstName; public string LastName; public long GuestNo; public string Notes; public GroupMember(int primaryKey, string firstName, string lastName, long guestNo, string notes) { PrimaryKey = primaryKey; FirstName = firstName; LastName = lastName; GuestNo = guestNo; Notes = notes; } }
static class DoCase { internal static void Execute( params Case[] cases) { foreach( Case aCase in cases ) { if (aCase.Predicate()) { aCase.Action(); break; } } } } class Case { internal readonly Func<bool> Predicate; internal Action Action; internal Case(Func<bool> predicate, Action action) { Predicate = predicate; Action = action; } internal Case(Action action) { Predicate = () => true; Action = action; } }