switch statement expressions

  • Thread starter Thread starter John
  • Start date Start date
J

John

Is there a way in c# to have a switch statement with multiple expressions
like in VB.net?

Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem
Code:
Case expression, expression2
[code]
End Select
 
Hi,

There are a number of ways you can achieve the same flow.

Switch( e.item.itemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
Code:
break;
case ListItemType.SelectedItem:
[code]
break;
case ListItemType.Header:
case ListItemType.Footer:
[code]
break;
default:
[default catch all code if required]
break;
}

Alternativly you might want to jump from one case to another for example

Switch( e.item.itemType )
{
case ListItemType.Item:
[code]
break;
case ListItemType.AlternatingItem:
[code]
goto case ListItemType.Item;
case ListItemType.EditItem:
[code]
break;
case ListItemType.SelectedItem:
[code]
goto case ListItemType.EditItem;
default:
[default catch all code if required]
break;
}

Here the item will contain code for items. The AlternatingItem will contain code for the alternating item such as a colour then jump to the Item case statement before exiting the switch. Obviously you have to think about the code you are adding i.e. if you set the colour in the AlternatingItem and then call Item where it sets the default colour this will replace the one set in the AlternatingItem. Ultimatly you would not set the colour here as the control has the relevant properties but this just demonstrates two possible methods

- Mike
 
Back
Top