Written By TheVBProgramer.
The GoTo statement can be used to branch to statements within a Sub or Function procedure. We have all heard the lectures on the "evils" of using GoTo statements, so I'm not going to rehash all that here. In general, use of modern programming constructs such as the ones previously presented (Do/Loop, If/Then/Else, Select Case, etc.) virtually eliminate the need to use GoTos. You'll find that the only time that the use of GoTo is required is when setting up error-handling code (error-handling is discussed in a later topic).
The rules for using GoTo are as follows:
· The destination of a GoTo statement must be a line label or line number that resides within the same Sub or Function procedure as the statement that issues the GoTo (i.e., you cannot "go to" a line in a Sub or Function other than the one you are currently in).
· The name of the line label follows the same rules as that for naming a variable. The line label must begin in the first column of the line and must be followed by a colon(:).
· If a line number is used, it must begin in the first column of the line.
Following is a version of the factorial program presented earlier that uses GoTos instead of the For/Next loop for looping :
Private Sub cmdTryIt_Click()
Dim lngFactorial As Long
Dim intInputNbr As Integer
Dim intLoopCtr As Integer
intInputNbr = Val(InputBox("Enter a number:", "GoTo Demo"))
lngFactorial = 1
intLoopCtr = 1
Loop_Start:
If intLoopCtr > intInputNbr Then GoTo 10
lngFactorial = lngFactorial * intLoopCtr
intLoopCtr = intLoopCtr + 1
GoTo Loop_Start
10 ' End of loop
Print CStr(intInputNbr); "! = "; lngFactorial
End Sub
Download the VB project code for the example above here.
In earlier versions of BASIC, the only way you could make your programs modular was to break your program up into "subroutines" and use GoSub to execute that subroutine and return back to the calling statement. GoSub was included in VB to maintain backward compatibility; it is not a particularly "bad" construct to use, but its use is generally discouraged – it is recommended that Sub and Function procedures be used instead.
The rules for using GoSub are as follows:
· The destination of a GoSub statement must be a line label or line number that resides within the same Sub or Function procedure as the statement that issues the GoSub (i.e., you cannot "GoSub" to a line in a Sub or Function other than the one you are currently in). In effect, using GoSub lets you have "subs within a sub" (with the exception that you cannot pass parameters to a "GoSubbed" routine).
· If a line label is used, the name of that line label must follow the same rules as that for naming a variable. The line label must begin in the first column of the line and must be followed by a colon(:).
· If a line number is used, it must begin in the first column of the line.
· Once the destination of the GoSub is reached, a Return statement will return control to the statement after the one that issued the GoSub.
Following is code to demonstrate the use of Gosub/Return. Note that the use of the Exit Sub statement is required to prevent "falling through". The code in this VB Sub is set up in a manner similar to the way a program would have to be written in QBASIC (one of the ancestors of VB): the three GoSubs formulate the main control flow of the "program" and the Exit Sub ends the "program". Note: Without the Exit Sub, we would fall through to SubroutineA, and when the Return statement associated with SubroutineA hit, a "Return without GoSub" error would be generated.
Private Sub cmdTryIt_Click()
GoSub SubroutineA
GoSub SubroutineB
GoSub 1000
Exit Sub
SubroutineA:
Print "Hey kids, I'm in Subroutine A"
Return
SubroutineB:
Print "Hey kids, I'm in Subroutine B"
Return
1000
Print "Hey kids, I'm in Subroutine 1000"
Return
End Sub
Download the VB project code for the example above here.
console app
Ok so hi. I have coded this to goto the view label when I type in view, but It always goes to the help label. I don't know why so please help the exact same thing happens when I type in delete to go to the delete label. this is the code:
usin stands for user input
usde stands for user delete
answer handles are you sure messages
Module Module1
Sub Main()
Dim UsIn As New Object
Dim Usde As New Object
Dim answer As New Object
Dim usvi As New Object
Main:
Console.WriteLine("^ Prompt")
UsIn = Console.ReadLine()
'
If UsIn = "cls" Then
Console.Clear()
GoTo Main
ElseIf UsIn = "help" Then
GoTo Help
ElseIf UsIn = "delete" Then
GoTo delete
ElseIf UsIn = "view" Then
GoTo view
Else
GoTo usinerror
End If
Help:
'displays help
Console.WriteLine("^ prompt help")
Console.WriteLine("Key in cls to clear the screen")
Console.WriteLine("Key in view to view a text file's contents")
Console.WriteLine("Key in delete to delete a file")
Console.ReadLine()
GoTo Main
delete:
Console.WriteLine("Key in the path of the file you would like to delete.")
Usde = Console.ReadLine()
Console.WriteLine("Are you sure you want to permanently delete" & Usde & "?")
answer = Console.ReadLine()
If answer = "yes" Then
My.Computer.FileSystem.DeleteFile(Usde)
GoTo main
ElseIf answer = "no" Then
GoTo main
End If
view:
Console.WriteLine("Key in the path of the file you would like to view")
usvi = Console.ReadLine()
Dim FILE_NAME As String = usvi
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Console.WriteLine(objReader.ReadToEnd)
console.readline()
GoTo Main
usinerror:
console.writeline("error")
console.readline()
GoTo Main
End Sub
End Module
goto statement
Dim marks as integer
marks=Text1.Text
if marks >50 then
Goto Ln1
else
Goto Ln2
end if
Ln1:
lbl1.caption="Pass"
Ln2
lbl1.caption="Fail"
...............
when i run this i always get" Fai" as outputl no matter what mark i give whats wrong with this code. pl help me
That's simple
Oh, that's quite simple:
When you GOTO Ln1, the line
lbl1.caption = "Pass"is executed. Then VB does not know why it should not execute the next line as well and sets the caption to "Fail".
Try this:
Dim marks as integermarks=Text1.Text
if marks >50 then
Goto Ln1
else
Goto Ln2
end if
Ln1:
lbl1.caption="Pass"
Exit Sub
Ln2:
lbl1.caption="Fail"
If you're in a function, use Exit Function instead.
Code
If TextBox1.Text => 50 Then
Label1.Text = "Pass"
Else
Label1.Text = "Fail"
End If
visual basic 6.0
can i ask....i didnt know code of RETURn..pls answer..tnx..smch
can any1 help me.. cpu
can any1 help me..
cpu scheduling spf program..
or formula for comparing 5 numbers that will input to 5 textbox then in the smallest will out to the 1st label then the second smallest to the 2nd label and up to the highest..
tnx..:P
Goto??
can you have the goto number at the begiing of the sub?
so...
eg.
private sub
10:
...
....
.....
......
go to 10.
end sub
go to other sub
how can go to line in other sub
tnks
How to go to line number in other subs (other command button)
How to go to line number in other subs (other command button)?
My code is something like this,
Private Sub Coef_Click()
if x>10, then
text = Good
endif
go to 25
end sub
Public Sub uscs_Click()
25:
text2= "recalculate"
endsub
It is not running. Any help is highly appreciated.
Hi the problem that you have
Hi
the problem that you have it's because you are sending the 25 to other sub and the sentence go to can't be used in that way, you can use it only in the same sub where you are using it
i hope that my comment can help you :)
good luck
goto with Variable
please i need help
Dim x as integer
x=inputbox()
goto x
33
.
.
.
88
.
.....
..............
.
44
j
.....
...........
for example
i think that it doesn't
i think that it doesn't going to work, but you can use the sentence select case what can help you like you want. you can use select to like this example:
Dim var as integer
var= inputbox()
select case var
case 33
.
.
case 88
.
.
.
case 90
..
.
end select
i hope this can help you :)
an answer
if you want to use "GOTO" in select case... you can just simply use "GOTO" in a select case.
can i ask how use goto with
can i ask how use goto with a Variable for example
Dim x as Integer
x=inputbox()
goto x
1 ....
2......
3.....
as i understand your code.
as i understand your code. hope this will help you...
for example you're going to add so lets put your code to an ADD button named cmdAdd
Private Sub cmdAdd_Click()
On Error GoTo hell
hell:
Dim x As Integer
If (Text1.Text = " ") = True Then
x = InputBox( )
Text1.Text = x
End If
as i understand you code i
as i understand you code i hope this will help you..
dim x as Integer
If (Text1 = " ") = True Then
x = InputBox()
Text1 = x
End If
a question
Hi!
I have a question. What should we do if we want to use "GOTO"
in select case?
(i.e we want to repeat the select case untill one of the cases
happen)
Thank you very much. Please send me the answer.
Post new comment