Solved

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

Posted on 2006-07-20
12
694 Views
Last Modified: 2008-03-06
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?
0
Comment
Question by:kak39
  • 5
  • 4
12 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17146518
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.
0
 

Author Comment

by:kak39
ID: 17146631
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.

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17146671
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
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:kak39
ID: 17147039
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



0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17147094
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

0
 

Author Comment

by:kak39
ID: 17147745
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.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17147791
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
0
 

Author Comment

by:kak39
ID: 17147872
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

------------------------
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 250 total points
ID: 17147955
"Exit For" will cause the j loop to terminate, dropping you to Next i. NextStatement will only run if j<>0.

If nextStatement represents a lot of code, you might want to break it out into a subroutine.

For i
  For j
    if j = 0 Then Exit For
    NextStatement
  Next j
Next i
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question