Solved

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

Posted on 2006-07-20
12
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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