Switch<T> Activity Designer

The Switch<T> activity evaluates a specified expression and executes the activity from a collection of activities whose associated key matches the value obtained from the evaluation.

The Switch<T> activity designer is used to create and configure a Switch<T> activity in the Windows Workflow Designer.

The Switch<T>Activity

A Switch<T> activity contains an Expression and a dictionary of Cases. Each case in the dictionary consists of a pair that contains a key and an activity that serves as its corresponding value. The Switch<T> activity evaluates the Expression and compares it against each of the keys. If a match is found, the corresponding activity is executed. Only one match is possible because dictionary keys must be unique according to the type of equality defined by the equality comparer of the dictionary. If no match is found, the Default activity is executed.

How to use the Switch<T> Activity Designer

The Switch<T> activity designer can be found in the Control Flow category of the Toolbox, which is accessed by clicking the Toolbox tab on the Workflow Designer (Alternatively, select Toolbar from the View menu, or CTRL+ALT+X.) After dropping it into the Workflow Designer, it displays the Select Types dialog to let the user specify the generic type T used in Switch<T> activity. The default value is Int32. Once the generic type T has been selected, a Switch<T> designer is added into the workflow designer.

Following are the properties of Switch<T> designer. All of these properties can be edited in property grid. Some of them can also be edited on designer surface.

The following table shows the most useful Switch<T> properties and describes how they are used in the designer.

Property Name Required Usage
DisplayName False Specifies the friendly name of the Switch<T> activity designer. The default value is Switch<Int32>. The value can be edited in the Properties window or directly on the designer header.

Although the DisplayName is not strictly required, it is a best practice to use one.
Expression True Specifies the expression used to compare to the keys in the cases collection to determine which case to execute.
Default Specifies the activity executed if no match is found. Click the Add an activity button on the designer to open the Default box where the activity can be dropped.
Cases Specifies the cases to be evaluated. To add a case, click the Add new case button at the bottom of Switch<T> designer. The button changes to a textbox (combo box if the generic type selected when adding the Switch<T> is String or Enum). After adding a key in the Case value box, the case area expands and an activity can be dropped where the hint text “Drop activity here” to define the execution logic for the case.

Multiple cases can be added as long as the case keys are not duplicated. Otherwise, an error dialog displays reporting the specified case key already exists and that you must choose a different key. In the Switch<T> designer, only one case area can be in expanded view at a time. If a case area is in collapsed view, clicking the case area expands it. Notice that for a collapsed case, the designer shows the display name of the activity inside the case on the right side if there is any. Otherwise, it shows the Add an activity button that expands the case if you click it and lets you add an activity.

Clicking the key of existing case changes the key from a label into a textbox so that you can edit the case key.

There are 2 ways to delete a case:

  1. Select the case and delete it.

  2. Select the case, right-click to display the context menu and select Delete.

    Note that you must select the case itself to delete it. Selecting and deleting the activity inside a case only deletes the activity not the case.

See Also

Control Flow