-
Sep 1st, 2022, 01:06 PM
#1
Thread Starter
New Member
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
-
Sep 1st, 2022, 01:25 PM
#2
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.
-
Sep 1st, 2022, 02:08 PM
#3
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.
-
Sep 1st, 2022, 09:33 PM
#4
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.
-
Sep 1st, 2022, 09:35 PM
#5
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:
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
If gotolable = "G5" Then Return
DGVItemDetail(10, nRow).Value = DGVItemDetail(4, nRow).Value * DGVItemDetail(9, nRow).Value
If gotolable = "G4" Then Return
'Etc.
-
Sep 2nd, 2022, 06:43 AM
#6
Re: Need Variable Label in GOTO Label statement and/or alternative to GOTO Label.
Originally Posted by jmcilhinney
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:
CallLineCalculations(1, ABCD)
Last edited by jmcilhinney; Sep 2nd, 2022 at 07:46 AM.
-
Sep 2nd, 2022, 10:15 AM
#7
Thread Starter
New Member
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.
-
Sep 2nd, 2022, 10:17 AM
#8
Thread Starter
New Member
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
-
Sep 2nd, 2022, 10:19 AM
#9
Thread Starter
New Member
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.
-
Sep 2nd, 2022, 10:24 AM
#10
Thread Starter
New Member
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
-
Sep 5th, 2022, 08:03 PM
#11
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|