5

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

13
  • 3
    Possible duplicate of VB.NET Stacking Select Case Statements together like in Switch C#/Java
    – David Pine
    Commented May 10, 2016 at 14:29
  • 3
    Not a duplicate as it doesn't answer the question... Commented May 10, 2016 at 14:29
  • Actually, you can't do this in c#. In order for the fall through to work, there can't be any statements between the cases. Also, the possible duplicate answers your question exactly. Commented May 10, 2016 at 14:32
  • C# doesn't allow "fallthrough" for the non-empty case.
    – spender
    Commented May 10, 2016 at 14:33
  • C# Allows fallthrough with non empty cases!? Commented May 10, 2016 at 14:33

1 Answer 1

7

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

8
  • I think the OP mentioned c, not c# although the post has been updated quite a bit so maybe c# was originally mentioned?
    – Igor
    Commented May 10, 2016 at 14:42
  • @Igor Yes, originally it was C#.
    – spender
    Commented May 10, 2016 at 14:43
  • Yeah but it's the same concept... I was wondering if there was an equivalent to that in VB .Net... Commented May 10, 2016 at 14:43
  • By the way - it's worth noticing that goto case in C# can jump to any case (upwards too) and has no safeguards as seen in this infinite switch fallback loop Commented May 10, 2016 at 14:46
  • 2
    The day a goto abuse anwser is a good anwser, hell frozen over. Commented May 10, 2016 at 14:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.