Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is it possible to simplify this code?
Message
De
10/09/2013 04:40:37
 
 
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:
01582661
Vues:
79
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;
		}
	}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform