Selecting cases in C# and VFP

C#

Switch, Case, Default

VFP

Do Case, Case, Otherwise

There are two big differences between the case selection structures in the two languages and they are a common source of bugs and difficulties with conversions:

  • C sharp implements a Java-style 'fall through' from one case to the next whereas FoxPro implements the Pascal-style where only the statements associated with the first true test are executed.
  • C sharp applies the test against the same variable in all the cases whereas FoxPro can examine a different variable in each case.

C# Syntax Notes

The switch statement in C# is followed by an expression and then by a series of case statements, one for each of the values of the expression that are to be processed. You must remember to include the break at the end of the block of code for each case to transfer execution to the end of the switch block.

switch(i);
  {
  Case 1:
    Console.WriteLine("one");
    break;
  Case 2:
    Console.WriteLine("two");
    break;
  default:
    Console.WriteLine("many");
    break;
  }

Note that C# does not allow the VB style of stating a range of values - case1to4. Instead you have to quote each case explicitly. The "fall through" mode of execution means that you can give a series of empty cases followed by the code which is to apply to all of them:

  Case 1:
  Case 2:
  Case 3:
  Case 4:
    Console.WriteLine("one to four");

One final point to note about the C# switch syntax is that each case is followed by a colon, not by the usual semicolon. This is because each is a label and C# actually supports the goto statement.It is possible, but not advisable, to jump directly from any one case to another.

VFP Syntax Notes

Each case in a FoxPro structure has its own independent logical test:

Do Case
   Case Price > 1000
     ? "Delivery is free over £1,000."
   Case Country = "UK"
     ? "Delivery is free within UK"
   Otherwise
     ? "Charge for delivery."
EndCase

The conditions for each case in a VFP structure can be anything that returns a boolean, even if this requires a call to an external function or a request for input from the user. This flexibility in the Do Case structure makes up for the lack of else if chains in FoxPro.

If  |  Language index  |  Loops

Related Items

Site Map

Site map of the Alvechurch Data site

Read More

VFP and C#.

C# is very different from Visual FoxPro so conversion from one language to the other is difficult.

Read More

Comments in C# and VFP

Adding comments to programs in C# and Visual FoxPro

Read More

Constants in C# and VFP

Declaring constants in C# and Visual FoxPro

Read More

Variables in C# and VFP

Declaring variables in C# and Visual FoxPro

Read More