Since you cannot implicitly proceed to the next case, you must do this explicitly using the goto . 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 be better to reorganize it using if :
MediaStream GetPhotoMediaStream(PhotoSize photoSize, ) { 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; }
source share