Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# switch Gripe
Message
From
04/01/2009 07:27:51
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01370558
Message ID:
01370876
Views:
43
>>>>>>Why in the world must a switch statement need an integer???
>>>>>>
>>>>>>Other languages are much more flexible in their CASE structures, so why would MS restrict the evaluation of the switch to an int????
>>>>>
>>>>>See Naomi's answer, but also be aware:
>>>>>
>>>>>If your trying to compare a switch statement to a VFP "DO CASE", they are very different. To write a VFP style case statement in C# use "else if" .
>>>>
>>>>Very different how? To me they seem quite similar. They are both structured as a series of checks of some value. When a match is found, take some action and exit. Both provide for a default action if no match is found.
>>>
>>>In VFP DO CASE you can put any condition, even function evaluation. In C# switch you analyze one variable.
>>
>>
>>You can simulate the vfp do case with a delegate
>>Disadvantage: all conditions are evaluated
>>Think I prefer the if - else if - else if
>>
>>
>>using System;
>>using System.Collections.Generic;
>>using System.Linq;
>>using System.Text;
>>
>>namespace GregoryAdam.Test
>>{
>>	public class CaseTest
>>	{
>>		public static void Main()
>>		{
>>			int result = 0;
>>			int a = 23, b = 27;
>>
>>			Case.Execute
>>				(
>>				new CaseOne( a==1, () => { result = 1;}),
>>				new CaseOne( b==2, () => { result = 2;}), 
>>				// otherwise
>>				new CaseOne( true, () => { result = 99;}) 
>>				);
>>
>>			Console.WriteLine("result = {0}", result);
>>			Console.ReadLine();
>>		}
>>	}
>>
>>	public static class Case
>>	{
>>		public static void Execute(params CaseOne[] cases)
>>		{
>>			for( int i = -1; ++i < cases.Length; )
>>				if (cases[i].Condition)
>>				{
>>					cases[i].Act();
>>					break;
>>				}
>>		}
>>	}
>>	public class CaseOne
>>	{
>>		public bool Condition;
>>		public Action Act;
>>
>>		public CaseOne(bool condition, Action act)
>>		{
>>			Condition = condition;
>>			Act = act;
>>		}
>>	}
>>
>>}
>>
>
>Neat!
>A fine example of how it is possible to bend the language to your will - and an excellent example of why not to do it :-}
>And I didn't know the Action delegate existed!
>Regards,
>Viv

hi Viv,

Sure you knew Action existed - maybe not 'by' name - it's just an anonymous method - member of the generic delegate family
I'm convinced that you have used it - see last example here http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

Actions are void
following are predefined - up to 4 generic args
delegate void Action()
delegate void Action<T> (T arg)
delegate void Action<T1, T2>(T1 arg1, T2, arg2)
delegate void Action<T1, T2, T3>(T1 arg1, T2, arg2, T3 arg3)
delegate void Action<T1, T2, T3, T4>(T1 arg1, T2, arg2, T3 arg3, T4 arg4)
Closely related - they return a result - also with a max of 4 generic args
[ I wonder why the TResult did not stay in the first position ]
delegate TResult Func<TResult>()  // http://msdn.microsoft.com/en-us/library/bb534960.aspx
delegate TResult Func<T, TResult>( T arg) // http://msdn.microsoft.com/en-us/library/bb549151.aspx
delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2)
delegate TResult Func<T1, T2, T3, TResult>( T1 arg1, T2 arg2, T3 arg3)
delegate TResult Func<T1, T2, T3, T4, TResult>( T1 arg1, T2 arg2, T3 arg3, T4 arg4)
Happy 2009
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform