Solved

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

Posted on 2006-07-20
12
704 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET Inline If statement 4 58
visual studio vb.net windows onkeyup 2 40
Format Transaction Number 19 50
Click print button programmatically on print showdialog 1 24
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ā€¦
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦

752 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