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

Click here to load this message in the networking platform