Martin Verjans
Martin Verjans

Reputation: 4806

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

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...

Upvotes: 5

Views: 720

Answers (1)

spender
spender

Reputation: 120548

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

Upvotes: 7

Related Questions