Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a VB .NET equivalent to C Fall Through switch?

This is not a duplicate of this question : VB.NET Stacking Select Case Statements together like in Switch C#/Java. The answer provided here does not answer my question. The answer there is stating that there is an automatic break in VB .Net, which I'm aware of. I'm asking if there's any workaround.

In C, it is possible to do something like this :

int i = 1;
switch (i) {
   case 1 :
     //Do first stuff
     break;
   case 2 :
     //Do second stuff
     //Fall Through
   case 3 :
     //Do third stuff 
     break;
}

Basically

  • If i is 1, app will do first stuff.
  • If i is 2, it will do second AND third stuff.
  • If i is 3, it will do only third stuff.

Since there is an auto break at the end of each Select case statement in VB .Net, does anyone know how to achieve this in VB .Net ?

In a nice and pretty way I mean...

like image 212
Martin Verjans Avatar asked May 10 '16 14:05

Martin Verjans


1 Answers

Your premise is wrong. In C# you can't fall through to the next case if the current case has statements. Trying to do so will result in a compilation error.

You can, however, (ab)use goto case to get this working.

switch(0)
{
    case 0:
        Console.WriteLine("0");
        goto case 1;
    case 1:
        Console.WriteLine("1");
        break;

}

VB.Net has no equivalent of goto case

like image 72
spender Avatar answered Nov 15 '22 06:11

spender