GoTo line

T

Tomas Andersson

Hi,

Does anyone know if it's possible to GoTo a line in a different sub

I have a program whith a loop. In this loop I would like to put a tag and if
something goes wrong with the execution in one of the subs or functions
called in this loop it should go back to the loop and continue with the next
execution.

I don't know if this makes sense so "code" below might be clearer


File A.VB
------------
sub Z
do
lable nextthingtodo
call sub X
loop
end sub


File B.VB
----------
sub X()
bla bla
bla bla bla
call sub ZZ
if error
go to nextthingtodo
end if
end sub

sub ZZ()
if error
GotTo nextthingtodo
end if
end sub
 
R

Rory Becker

Hello Tomas,
Does anyone know if it's possible to GoTo a line in a different sub

I ***REALLLY*** hope not

Without wishing to sound *too* negative :).... You really shouldn't do this.

Use of "goto" should be highly restricted.. There is almost always a better
way to achieve what got would otherwise do.

If you post some more specific code, then perhaps we can help you find a
better way to achieve what you're after
 
F

Family Tree Mike

Tomas Andersson said:
Hi,

Does anyone know if it's possible to GoTo a line in a different sub

I have a program whith a loop. In this loop I would like to put a tag and
if something goes wrong with the execution in one of the subs or functions
called in this loop it should go back to the loop and continue with the
next execution.

I don't know if this makes sense so "code" below might be clearer


File A.VB
------------
sub Z
do
lable nextthingtodo
call sub X
loop
end sub


File B.VB
----------
sub X()
bla bla
bla bla bla
call sub ZZ
if error
go to nextthingtodo
end if
end sub

sub ZZ()
if error
GotTo nextthingtodo
end if
end sub


Why not make "Goto nextthingtodo" be a call to a subroutine NextThingToDo()?
 
A

Armin Zingler

Tomas said:
Hi,

Does anyone know if it's possible to GoTo a line in a different sub

I have a program whith a loop. In this loop I would like to put a tag
and if something goes wrong with the execution in one of the subs or
functions called in this loop it should go back to the loop and
continue with the next execution.

I don't know if this makes sense so "code" below might be clearer


File A.VB
------------
sub Z
do
lable nextthingtodo
call sub X
loop
end sub


File B.VB
----------
sub X()
bla bla
bla bla bla
call sub ZZ
if error
go to nextthingtodo
end if
end sub

sub ZZ()
if error
GotTo nextthingtodo
end if
end sub

That's beyond how software works. It's even beyond how hardware works. In
your example, just write "Return". Without a real example, it's hard to say
how to replace it with real working code. In general, a sub does not know
from where it has been called. Change the sub being a function and return
any information you need. The calling procedure then can decide what to do.


Armin
 
P

Phill W.

Tomas said:
Does anyone know if it's possible to GoTo a line in a different sub

Not in any variant of VB, no.
I have a program whith a loop. In this loop I would like to put a tag and if
something goes wrong with the execution in one of the subs or functions
called in this loop it should go back to the loop and continue with the next
execution.

Depending on how badly things "go wrong", you can do this with ordinary
flow control logic or Exception Handling:


The "Flow Control" way:

Do While condition
If X() Then
...
End If

' "next iteration" code
Loop

Function X() as Boolean
Return ZZ()
End Sub

Function ZZ() as Boolean
If somethingIsNotRight Then
Return False
End If
...
Return True
End Sub


The "Exception Handling" way:

Do While condition
Try
X()
Catch( ex as Exception )
' Deal with ex
End Try

' "next iteration" code
Loop

' Note: we don't /have/ to do anything in this method
' (but we /do/ have that option, if we want to)
' By default, Exceptions will propagate up to the Catch
' block in the top-most method.
Sub X()
ZZ()
End Sub

Sub ZZ()
If somethingIsNotRight Then
Throw new Exception( ...
End If
...
End Sub

HTH,
Phill W.
 
A

Andrew Morton

Cor said:
Try to read something what Dijkstra wrote

http://en.wikipedia.org/wiki/Edsger_Dijkstra

....which links to
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html

What a load of waffling!

The usual explanation for why using goto is bad is simple: it makes code
difficult to maintain. The proof is easy: present a spaghetti-code program.

O.P: http://en.wikipedia.org/wiki/Spaghetti_code - although what they
suggest to be spaghetti code is somewhat lean. Try this:
http://www.atariarchives.org/basicgames/showpage.php?page=159

Andrew
 
B

Branco

Andrew Morton wrote:
The usual explanation for why using goto is bad is simple: it makes code
difficult to maintain. The proof is easy: present a spaghetti-code program.
<snip>

Actually, programming languages of yore (Basic and VB included, if I'm
not mistaken) allowed the goto to jump to any point in the procedure,
even to a label inside a control structure (If blocks, For blocks,
etc). This, of course, used to throw control flow through the
proverbial window. Modern languages know better and correctly prevent
this bizarre behavior.

Nowadays I don't see many imperative reasons to use goto's given
modern control flow structures, but I've seen it used in unexpected
places (Paul Vick -- one of the heads behind the language we know
today as VB.Net -- uses it in his proof of concept VB parser.
Really!).

Personally, I'd very much prefer a GoTo to another method -- yes, you
read it right. It seems to me such kind of "call transfer" could
simplify many algorithms (tail-call recursion, for example).

Regards,

Branco.
 
A

Andrew Morton

Branco said:
Actually, programming languages of yore (Basic and VB included, if I'm
not mistaken) allowed the goto to jump to any point in the procedure,
even to a label...

Whoa there! Label? No, no, what you want are line numbers :) And very small
line numbers at that, if you only have 1KB of RAM
(http://en.wikipedia.org/wiki/Zx81). Also, judicious choice of line numbers
can make the calculation of the line number for computed gosubs simpler.

Andrew
 
M

Martin H.

Hello Patrice,
Also for new code, you should never use goto/gosub. Use functions and subs
instead...

GoSub...Return does not exist in VB.NET anymore. :(

Best Regards,

Martin
 
B

Branco

Andrew said:
Whoa there! Label? No, no, what you want are line numbers :) And very small
line numbers at that, if you only have 1KB of RAM
(http://en.wikipedia.org/wiki/Zx81). Also, judicious choice of line numbers
can make the calculation of the line number for computed gosubs simpler.

lol, I just remembered one of my first personal Z80 assembly projects,
when I patched the MSX-Basic (from Microsoft), so it accepted labels
in place of line numbers. Now *that* was cool... (and yes, I'm *that*
old)

Regards,

Branco.
 
J

J.B. Moreno

Patrice said:
Actually I'm even surprised we have still goto ;-) and more surprised they
keyp goto and suppressed gosub ;-)

GoSub has been completely replaced by Sub/Function. While goto doesn't
have an equivalent.
 
C

Cor Ligthert

No problem, I don't discus about religion, this guy changed programming like
Darwin did with Natural science. But there is not reason that you should
believe one of those.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top