I was wondering if it was possible to conditionally break out of a case in a switch statement in C#. Take the following example.
MediaStream photoMediaStream = null;
switch (photoSize)
{
case PhotoSize.Normal:
if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
break;
}
case PhotoSize.Small:
if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
break;
}
case PhotoSize.Thumb:
if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
break;
}
}
Basically, if the conditional is true I want to do something and then break out of the switch statement, but if not I just want to fall through to the next case.
Switch Case In C In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon.
You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.
4) The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 5) The break statement is optional. If omitted, execution will continue on into the next case.
Since you can't implicitly fall through to the next case, you must do it explicitly, using the goto
statement. This is one of the rare cases where the use of this statement is acceptable...
MediaStream photoMediaStream = null;
switch (photoSize)
{
case PhotoSize.Normal:
if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
break;
}
goto case PhotoSize.Small;
case PhotoSize.Small:
if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
break;
}
goto case PhotoSize.Thumb;
case PhotoSize.Thumb:
if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
{
photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
break;
}
}
Anyway, it would probably be better to refactor it using if
statements:
MediaStream GetPhotoMediaStream(PhotoSize photoSize, /* whatever parameters you need... */)
{
if (photoSize == PhotoSize.Normal)
{
if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
{
return photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
}
photoSize = PhotoSize.Small;
}
if (photoSize == PhotoSize.Small)
{
if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
{
return photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
}
photoSize = PhotoSize.Thumb;
}
if (photoSize == PhotoSize.Thumb)
{
if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
{
return photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
}
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With