Results 1 to 11 of 11

Thread: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Hi, I have a sequence of Math Expressions (say from 1 to 10), which are interdependent. Based on condition, I need to start the working from different start positions (i.e. Line numbers) e.g. in case 0 - Expression 1 to 10 must be solved, in Case 4 - expression 4 to 10 must be solved and so on).
    I tried following code but failed due to challenge that "Label in GOTO Label statement can not be variable"
    May anyone suggest a solution?

    Private Sub XYZ(sender As Object, e As EventArgs) Handles SOME handle
    Dim ABCD As Integer = 0

    Select Case ABCD
    Case 0
    CallLineCalculations(1, "G0")
    Case 4
    CallLineCalculations(1, "G4")
    Case 5
    CallLineCalculations(1, "G5")
    End Select

    End Sub


    Private Sub CallLineCalculations(ByVal nRow As Integer, ByVal gotoLable As String)

    GoTo gotolable

    G0:
    DGVItemDetail(8, nRow).Value = Val(DGVItemDetail(6, nRow).Value) * Val(DGVItemDetail(7, nRow).Value) / 100
    DGVItemDetail(9, nRow).Value = Val(DGVItemDetail(6, nRow).Value) - DGVItemDetail(8, nRow).Value
    G4:
    DGVItemDetail(10, nRow).Value = DGVItemDetail(4, nRow).Value * DGVItemDetail(9, nRow).Value
    G5:
    DGVItemDetail(12, nRow).Value = DGVItemDetail(10, nRow).Value * DGVItemDetail(11, nRow).Value / 100
    DGVItemDetail(13, nRow).Value = DGVItemDetail(10, nRow).Value + DGVItemDetail(12, nRow).Value
    ...
    ...
    ...
    ...
    ...

    End Sub

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    There's some pretty problematic code there. First off, as written, your ABCD variable will always equal 0, so the "Case 0" branch will always execute.

    Secondly, in your CallLineCalculations Sub, if you just need to execute different code based on the value stored in a certain variable, use If-ElseIf statements, or use Select Case like you are already using in the code above. Don't try to invent "clever" nonfunctional hacks like "Goto someVariable".

    Good luck.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Looking at this some more, I'm inferring that, if G0 is passed, you want the code in the G0 sub to run, but then also the code in the G4 sub to run, and the code in the G5 sub to run as well. And if G4 is passed, you want the code in the G4 sub to run, and then the code in the G5 sub to run as well.

    Unlike the Switch statement in other languages, the VB.NET Select Case statement doesn't fall through the subsequent cases, unfortunately, otherwise it would be the perfect candidate for that type of logic.

    You could sort of fake it using a boolean variable and a series of isolated If statements, and it could look something like this:

    Code:
    Dim fallThrough As Boolean = False
    
    If gotoLable = "G0" Then
      'Replaces G0 sub
      'Add your code that should be done if G0 is passed here
    
      fallThrough = True
    End If
    If fallThrough OrElse gotoLable = "G4" Then
      'Replaces G4 sub
      'Add your additional code that should be done if G0 or G4 is passed here
    
      fallThrough = True
    End If
    If fallThrough OrElse gotoLable = "G5" Then
      'Replaces G5 sub
      'Add your additional code that should be done if G0, G4, or G5 is passed here
    
      fallThrough = True
    End If
    'And so on for any other conditions that might be needed
    Good luck.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Never use GoTo. You can ALWAYS write better code without it.

    Do these "GX" labels have any other significance or are they arbitrary? If the latter, just pass the number instead and then you can use a series if `If` statements to check whether the value is less than a threshold, e.g.
    Code:
        Private Sub XYZ(sender As Object, e As EventArgs) Handles SOME handle
            Dim ABCD As Integer = 0
    
            Select Case ABCD
                Case 0
                    CallLineCalculations(1, 0)
                Case 4
                    CallLineCalculations(1, 4)
                Case 5
                    CallLineCalculations(1, 5)
            End Select
    
        End Sub
    
    
        Private Sub CallLineCalculations(ByVal nRow As Integer, ByVal gotoLable As Integer)
            If gotoLable < 4 Then
                DGVItemDetail(8, nRow).Value = Val(DGVItemDetail(6, nRow).Value) * Val(DGVItemDetail(7, nRow).Value) / 100
                DGVItemDetail(9, nRow).Value = Val(DGVItemDetail(6, nRow).Value) - DGVItemDetail(8, nRow).Value
            End If
    
            If gotoLable < 5 Then
                DGVItemDetail(10, nRow).Value = DGVItemDetail(4, nRow).Value * DGVItemDetail(9, nRow).Value
            End If
    
            If gotoLable < 6 Then
                DGVItemDetail(12, nRow).Value = DGVItemDetail(10, nRow).Value * DGVItemDetail(11, nRow).Value / 100
                DGVItemDetail(13, nRow).Value = DGVItemDetail(10, nRow).Value + DGVItemDetail(12, nRow).Value
            End If
    ...
    ...
    ...
    ...
    ...
    
        End Sub
    BTW, notice that I have formatted the code for readability? Please do that in future when you post code snippets.

    Also, you spelled it correctly in your post so you obviously know that "Lable", as you have used in your code, is not a word.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    You could also do things backwards, e.g.
    vb.net Code:
    1. DGVItemDetail(12, nRow).Value = DGVItemDetail(10, nRow).Value * DGVItemDetail(11, nRow).Value / 100
    2. DGVItemDetail(13, nRow).Value = DGVItemDetail(10, nRow).Value + DGVItemDetail(12, nRow).Value
    3.  
    4. If gotolable = "G5" Then Return
    5.  
    6. DGVItemDetail(10, nRow).Value = DGVItemDetail(4, nRow).Value * DGVItemDetail(9, nRow).Value
    7.  
    8. If gotolable = "G4" Then Return
    9.  
    10. 'Etc.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Quote Originally Posted by jmcilhinney View Post
    Never use GoTo. You can ALWAYS write better code without it.

    Do these "GX" labels have any other significance or are they arbitrary? If the latter, just pass the number instead and then you can use a series if `If` statements to check whether the value is less than a threshold, e.g.
    Code:
        Private Sub XYZ(sender As Object, e As EventArgs) Handles SOME handle
            Dim ABCD As Integer = 0
    
            Select Case ABCD
                Case 0
                    CallLineCalculations(1, 0)
                Case 4
                    CallLineCalculations(1, 4)
                Case 5
                    CallLineCalculations(1, 5)
            End Select
    
        End Sub
    
    
        Private Sub CallLineCalculations(ByVal nRow As Integer, ByVal gotoLable As Integer)
            If gotoLable < 4 Then
                DGVItemDetail(8, nRow).Value = Val(DGVItemDetail(6, nRow).Value) * Val(DGVItemDetail(7, nRow).Value) / 100
                DGVItemDetail(9, nRow).Value = Val(DGVItemDetail(6, nRow).Value) - DGVItemDetail(8, nRow).Value
            End If
    
            If gotoLable < 5 Then
                DGVItemDetail(10, nRow).Value = DGVItemDetail(4, nRow).Value * DGVItemDetail(9, nRow).Value
            End If
    
            If gotoLable < 6 Then
                DGVItemDetail(12, nRow).Value = DGVItemDetail(10, nRow).Value * DGVItemDetail(11, nRow).Value / 100
                DGVItemDetail(13, nRow).Value = DGVItemDetail(10, nRow).Value + DGVItemDetail(12, nRow).Value
            End If
    ...
    ...
    ...
    ...
    ...
    
        End Sub
    BTW, notice that I have formatted the code for readability? Please do that in future when you post code snippets.

    Also, you spelled it correctly in your post so you obviously know that "Lable", as you have used in your code, is not a word.
    Actually, I should have looked more closely. I didn't actually notice the Select Case when I copied and edited that code. If you just pass in the number then you don't need the Select Case at all:
    vb.net Code:
    1. CallLineCalculations(1, ABCD)

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Thanks, this solution will work for me.
    Yes you are right. Case G0 must execute all the following Lines whereas Case G5 must directly jump to G5: Position and only lines after that will be executed.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Thanks, This suggestion is useful and workable. I myself wanted to avoid GOTO and now I have sufficient options, thanks to you and @OptionBase1

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    Thanks, This suggestion is useful and workable. I myself wanted to avoid GOTO and now I have sufficient options, thanks to you and @OptionBase1.

    And I will take care of Formatting next time.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    It is supposed to be working in a particular sequence and backward will not work,
    but your other suggestions are workable, thanks

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.

    There are neater ways to do this:-
    Code:
        Sub Main()
    
            Dim formulas As New List(Of Func(Of Integer, Integer))
    
            formulas.Add(Function(value As Integer) value * 2)
            formulas.Add(Function(value As Integer) (value * 10))
            formulas.Add(Function(value As Integer) (value / 9) + 15)
            formulas.Add(Function(value As Integer) (value ^ 2) + 12)
    
            Dim x = 12
    
            'Start using formulas from this index
            Dim start As Integer = 2
    
            For i = start To formulas.Count - 1
                x = formulas.Item(i)(x)
            Next
    
            Console.WriteLine(x.ToString)
            Console.ReadKey()
        End Sub
    The above uses lambdas and a List(Of T) to achieve the same thing. We have a list of formulas that uses the previous output as the input and we can start from anywhere in the list using a For...Next loop.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width