-2

I am building a game and I need to jump from Case to another. For example if the player is in the box 5 then the avatar needs to move to the box 9. Any help is highly appreciated.`

            Case 5
                OBJETO_ATERIOR = R4
                OBJETO_ACTUAL = R5
                OBJETO_ANTERIOR_INVERSO = R6

                'needs to jumpo to Case 9


                gif_loro.Visible = False
                gif_puppy.Visible = False
                gif_mono.Visible = False
                gif_oca5.Visible = True`
David Wilson
  • 4,369
  • 3
  • 18
  • 31
ripepe
  • 45
  • 5
  • this could be a duplicate of this https://stackoverflow.com/questions/820104/vb-net-switch-statement-goto-case – Any Moose Aug 14 '18 at 15:24
  • You could put your logic in different method and call the method based on the cases. Saying "move to box 9" is a bit confusing. – the_lotus Aug 14 '18 at 15:41

2 Answers2

1

Please take the time to read about [mcve]. As it stands, the code you supplied seems to have nothing useful to do with your question.

Though guessing at an answer, you could extract the code in the Case 9 block out into it's own Sub, and call that from the Case 9 block and from the appropriate point in the Case 5 block. An inelegant solution, but it may be your only choice.

So looking at an example, lets say your code looks something like this

Select Case x
    Case 1
        'case 1 code
    Case 2
        'case 2 code
    Case 3
        'case 3 code
    Case 4
        'case 4 code
    Case 5
        'case 5 code
        'jump to case 9 code
    Case 6
        'case 6 code
    Case 7
        'case 7 code
    Case 8
        'case 8 code
    Case 9
        'case 9 code
End Select.

You could create a sub that contains your code in the Case 9 block like so

 Private Sub Case9Code()
    'case 9 code
End Sub

and now you can rewrite your Select Case block as ..

Select Case x
    Case 1
        'case 1 code
    Case 2
        'case 2 code
    Case 3
        'case 3 code
    Case 4
        'case 4 code
    Case 5
        'case 5 code
        Case9Code()
    Case 6
        'case 6 code
    Case 7
        'case 7 code
    Case 8
        'case 8 code
    Case 9
        Case9Code()
End Select.
David Wilson
  • 4,369
  • 3
  • 18
  • 31
0

If I am getting what you are asking what you want is something like a goto statement (not available in VB.NET). This also indicates poor design and in turn I can offer an idea for a refactor.

The player object could contain a move sub that is called when moving spaces. This move could raise an event as well. The avatar object could contain a moveListener sub that listens for the move event to be raised and has a switch statement to determine how to handle the avatar movement. Since VB.NET events can contain params, the space the player moved to could be contained in the parameter; thus yielding an event handling design where the player movement is seen as an event and the avatar reacts to it.

Impurity
  • 1,037
  • 2
  • 16
  • 31