Solved

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

Posted on 2006-07-20
12
688 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now