skip to Main Content

In Visual Basic, is there a way to convert a string, entered on the command line, to a statement label? The intent is to execute only select portions of code. I’m working with a large program that includes many individual steps. For testing purposes I want to execute just one step without having to run through all of the previous steps.
I know how to extract the desired substring from the command line argument list.

So, given that string TestStep = "Step_3:"

somehow convert TestStep to point to label "Step_3:

GoTo TestStep
...
Step_1: 'do something
   GoTo Elsewhere
Step_2: 'do something else
   GoTo Elsewhere
Step_3: ' do the thing we want
   GoTo Elsewhere
Step_4: ' do other stuff
   GoTo Elsewhere
...

I’ve looked in "Visual Basic.NET" by Jesse Liberty and "VB.NET Language Pocket Reference" by Roman, Petrusha & Lomax without success.
Of course, just trying
GoTo TestStep where TestStep is a string results in Visual Studio responding: Label "TestStep" not defined

2

Answers


  1. I had a similar problem with a large and complicated Select..Case block that I didn’t like due to potential complexity. I solved it by using Action variable type and a Dictionary of function pointers as below.

    <TestMethod>
    Public Sub TestMethod1()
    
        Dim stepsToExecute As Dictionary(Of String, Action) = CreateStepsArray()
    
        Dim testStep As Action = stepsToExecute("Sub1")
        testStep()
        testStep()
        testStep = stepsToExecute("Sub2")
        testStep()
    
        testStep = stepsToExecute("Sub3")
        testStep()
    
    End Sub
    
    Private Function CreateStepsArray() As Dictionary(Of String, Action)
        Dim functionsList As New Dictionary(Of String, Action) From
        {
            {"Sub1", AddressOf Sub1},
            {"Sub2", AddressOf Sub2},
            {"Sub3", AddressOf Sub3}
        }
    
        Return functionsList
    End Function
    
    Private Sub Sub1()
        Debug.WriteLine("Sub 1")
    End Sub
    
    Private Sub Sub2()
        Debug.WriteLine("Sub 2")
    End Sub
    
    Private Sub Sub3()
        Debug.WriteLine("Sub 3")
    End Sub
    

    When the test is run, it will produce this output:

    Sub 1
    Sub 1
    Sub 2
    Sub 3
    
    Login or Signup to reply.
  2. The lightest touch that I can think of would be something based on Select Case which would probably also require labels and GoTos to jump between cases. My idea is that if you started with something like this (not actually executable VB code, more like a VB-Fortran hybrid pseudocode):

    Dim target As String
    Dim label As Object
    
    Assign target To Label 'Hypothetical nonexistent Fortran-style "assigned GoTo"
    
    GoTo Label
    
    Qwerty:
    ' Stuff to do under one label
    
    Dvorak:
    ' Stuff to do under another
    
    Uiop:
    ' Stuff to do under a third
    

    Then maybe it could convert to something like this:

    Dim target As String
    
    Select Case target
        Case "Qwerty"
            'Stuff
            'If (and only if) it should fall through
            GoTo Dvorak
    
        Case "Dvorak"
    Dvorak:
            'Stuff
            'Optional fall-through
            GoTo Uiop
    
        Case "Uiop"
    Uiop:
            'Stuff
    End Select
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search