Link to home
Start Free TrialLog in
Avatar of kak39
kak39

asked on

Removing GOTO: Statements in For-Loops (VB6 -> VB.NET)

First is there any performance increase on getting rid of GOTO: statements in VB6->VB.NET code?

If so, what's the best way to convert something like this:

--------------------

    For i = 0 to 10

          If num.values = 0 Then GoTo BadValue

          If numvalues = 1 Then GoTo NextStatement

     BadValue:
           BadValueFunction()

      NextStatement:
           Next

----------------------------------------------

This is just a quick example. Is there a good way to get rid of these GoTo Jumps?
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

GOTO is bad... it is a holdover from functional programming. .NET is object-oriented.

Looking at your code sample, BadValue is executed only if num.Values = 0... then you go to the next statement anyhow.

For as Integer= 0 to 10
  If num.values = 0 Then BadValueFunction
Next

Private Sub BadValueFunction()
End Sub

Once the code in BadValueFunction is executed, it will return you to the for..next loop.
Avatar of kak39
kak39

ASKER

Would there be any speed in execution if GOTO were removed? And not just to keep to the object-oriented arch?

Also, I under stand how to make that a Function call, but how would you do something like this:

if num.values = 1 Then GoTo NextStatement

( THERE IS A BUNCH OF OTHER CODE HERE I DON'T WANT IT TO GO THROUGH)


NextStatement:
   Next

------------------------
The Goto works good here because I jump over the code below that if-statement and go to the next loop iteration.

The biggest advantage probably won't be performance. The biggest advantage will be eliminating spaghetti code that is hard to follow logically.

For instance, in the case below, you can easily tell that the code in the If...Then block will only execute if the value is not 1. Gotos obscure the logical flow, and will make it tougher to maintain the application over the long term. Granted, someone used to thinking in gotos may be able to understand the code readily, but those people are getting to be rare :)

For as Integer= 0 to 10
if num.values <> 1 Then
( THERE IS A BUNCH OF OTHER CODE HERE I DON'T WANT IT TO GO THROUGH)
End If
Next
Avatar of kak39

ASKER

Bad example on my part, sorry. It won't allow me to do that because it's actually a nested For-Next Loops involved.
----------------
For
      For
            If num.values = 1 Then GoTo NextStatement

     Next

NextStatement:
Next

------------------

it wont let me do:

For
      For
           If num.values <> 1 Then

      Next

           End if
Next



Right.. you need to move the End If into the same structure that the If is in

For
      For
           If num.values <> 1 Then

           End if
      Next
Next

Avatar of kak39

ASKER

Yes, but that will only work if there is no code in the outside loop that doesn't need to be executed. Here's the problem:

For
    For
        If num.values <> 1 Then

        End If
    Next

     { THIS CODE SHOULD NOT EXECUTE if num.values <> 1 in first loop }

Next
   
-----------------------

This problem was orginally avoided by doing the GoTo directly to the last Next in the outside loop and skipping over that code in the brackets. I guess I could do another if-statement there, but seems redundent.
So you need to solve the problem here? (You didn't show which For set num)

For i
For j
If j=? then
End If
Next j

Do something based on j
Next i
Avatar of kak39

ASKER

if this command were availble it would do what I want

if j = 0 then NEXT I

--------------------------
This code works:

For i
For j
if j = 0 Then GoTo NextStatement
Next j
NextStatement:
Next i

------------------------
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial