Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is it possible to simplify this code?
Message
 
 
À
10/09/2013 10:32:23
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01581943
Message ID:
01582701
Vues:
46
>>>>>Hi everybody,
>>>>>
>>>>>In VFP I would use CASE expression. Is there any way to simplify this code?
>>>>>
>>>>>
>>>>>     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;
>>>>
>>>>
>>>>If you have many cases or the code per case gets bigger you can use a dictionary of delegates
>>>>It requires some code to set it up - in your case stick to switch - but maybe for future use
>>>>
>>>>I recently had a case with about 30 possibilities where each case calls a method
>>>>
>>>>
>>>>In your case you would have - never mind the class definitions of MKVP and GroupMember - had to test
>>>>
>>>>
>>>>	static class Test_DictionaryDelegates
>>>>	{
>>>>		delegate void ChangeGroupMember(GroupMember groupMember, MKVP mkvp);
>>>>
>>>>		static Dictionary<string, ChangeGroupMember> Actions = new Dictionary<string, ChangeGroupMember>()
>>>>		{	 {"PRI_KEY", ChangePrimaryKey}
>>>>			,{"FIRST", ChangeFirstName}
>>>>			,{"LAST", ChangeLastName}
>>>>			,{"GSTNO", ChangeGuestNo}
>>>>			,{"NOTES", ChangeNotes}
>>>>		};
>>>>
>>>>		//methods per case
>>>>		static void ChangePrimaryKey(GroupMember groupMember, MKVP mkvp)
>>>>		{
>>>>			groupMember.PrimaryKey = Convert.ToInt32(mkvp.Value);
>>>>		}
>>>>		static void ChangeFirstName(GroupMember groupMember, MKVP mkvp)
>>>>		{
>>>>			groupMember.FirstName = mkvp.Value;
>>>>		}
>>>>		static void ChangeLastName(GroupMember groupMember, MKVP mkvp)
>>>>		{
>>>>			groupMember.LastName = mkvp.Value;
>>>>		}
>>>>		static void ChangeGuestNo(GroupMember groupMember, MKVP mkvp)
>>>>		{
>>>>			groupMember.GuestNo = Convert.ToInt64(mkvp.Value);
>>>>
>>>>		}
>>>>		static void ChangeNotes(GroupMember groupMember, MKVP mkvp)
>>>>		{
>>>>			groupMember.Notes = mkvp.Value;
>>>>		}
>>>>
>>>>		// it happens here
>>>>		internal static  void  Go()
>>>>		{
>>>>			GroupMember groupMember = new GroupMember(1, "Gregory", "Adam", 234, "none");
>>>>			MKVP mkvp = new MKVP("First", "Alan");
>>>>
>>>>			ChangeGroupMember action;
>>>>			
>>>>			if( !Actions.TryGetValue(mkvp.Key.ToUpper(), out action))
>>>>				throw new NotImplementedException(string.Format("Don't know what to do with {0}", mkvp.Key));
>>>>
>>>>			action(groupMember, mkvp);
>>>>
>>>>		}
>>>>	
>>>>	}
>>>>
>>>>
>>>>	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;
>>>>		}
>>>>	}
>>>>
>>>I like that tho, given my druthers, I'd prefer an enum as the dictionary key:
 delegate void ChangeGroupMember(GroupMember groupMember,string s);
>>>
>>>        private static void Main(string[] args)
>>>        {
>>>
>>>            var actions = new Dictionary<Action, ChangeGroupMember>
>>>                {
>>>                    {Action.PrimaryKey, ChangePrimaryKey},
>>>                    {Action.First, ChangeFirstName},
>>>                    {Action.Last, ChangeLastName}
>>>                };
>>>
>>>            GroupMember groupMember = new GroupMember(1, "Gregory", "Adam", 234, "none");
>>>            MKVP mkvp = new MKVP(Action.First, "Alan");
>>>            actions[mkvp.Key](groupMember,mkvp.Value);
>>>        }
>>>although you'd have to ensure that the dictionary had an entry for all possible enum values......
>>
>>I like enums as well - but this looks like a straight vfp code conversion - that enum should have been introduced somewhere up the chain
>>[ and in vfp should have been a #define ACTION_PRIMARYKEY 1 etc]
>>
>>
>>Speaking of Action I could have used an Action< T, T >
>>
>>		static Dictionary <string, Action<GroupMember, MKVP>> Actions = new Dictionary<string,Action<GroupMember,MKVP>>()
>>		{	 {"PRI_KEY", ChangePrimaryKey}
>>			,{"FIRST", ChangeFirstName}
>>			,{"LAST", ChangeLastName}
>>			,{"GSTNO", ChangeGuestNo}
>>			,{"NOTES", ChangeNotes}
>>		};
>>
>Even better :-}

You guys should team up! Team Europa? European Software? Two Guys Coding? These are all freebies, no licensing fees due.

There is an actual compliment to both of you in there if you dig through my lame sense of humor.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform