GoTo ???

GoTo ???

Anonymous
Not applicable
242 Views
2 Replies
Message 1 of 3

GoTo ???

Anonymous
Not applicable
How do I use the GoTo command When I do it I am told that the line numer is not yet defined but I can not figure out how to define the line number.
0 Likes
243 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Sub TestGoto()
GoTo SkipLine
MsgBox "This won't show"
SkipLine:
MsgBox "This will show"
10 If MsgBox("Again? ", vbOKCancel) = vbCancel Then Exit Sub
GoTo 10
End Sub
0 Likes
Message 3 of 3

Anonymous
Not applicable
Please pardon the shout but

 

NO NO NO

 

DO NOT EVER USE THE GOTO UNLESS YOU ARE ERROR
HANDLING.

 

off the soapbox

 

ie

 

sub test()

on error goto test_error

    dim iTest as
integer

    iTest = "a"

    exit sub

test_error:

    msgbox "Error occured.",
vbCritical

end sub

 

or

 


sub test()

on error resume next

    dim vPt as Variant

RETRY:

    vPt = thisdrawing.getpoint (, "Pick point: ")

    if isempty(vpt)=true then goto RETRY

end sub

 

The second example is still a error handler because in the AC world, if a
user right clicks (or presses enter) that would be a error. This would catch the
'error' condition and not allow the user to exit without making a pick. I do not
think that VB still supports line numbers fully. The better way to go is to use
a label, as in the first example. Along the opening line, if you goto someplace
out of the sub (which you may not be able to) that is the worst programming
practice possible. If you need to go outside the sub, with a expected return,
just use another sub, or a function. In the first example, after the message
box, you could resume next, this would return your code to the next line after
the error jump. Be forewarned notice the exit sub before the test_error, if you
do not do this, the code in test_error *WILL* be executed. Maybe good, maybe
bad. Unfortunately, this is also the only way to handle errors in vb until
you(we) move to .net.

 

HTH, and that I was not too harsh.

 

Chris


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
How
do I use the GoTo command When I do it I am told that the line numer is not
yet defined but I can not figure out how to define the line
number.
0 Likes