Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is it possible to simplify this code?
Message
De
10/09/2013 06:11:55
 
 
À
10/09/2013 05:50:34
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:
01582671
Vues:
57
This message has been marked as a message which has helped to the initial question of the thread.
>>>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}
		};
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform