|
-
Apr 4th, 2001, 05:41 PM
#1
Okay, this is strange, but I don't know enough about GoTo Subs to answer it myself.
I want my variable dOutput to go to certain Subs if it's a certain number. I know my dOutput gets a value, but when it gets to the following part, it seems to go off track.
The weird thing is too that when dOutput =1 Then it does what it is supposed to, when dOutput=3 Then it does what it is suppsed to, but when dOutput=2 Then calls on both subs, Time and Chart, for some reason.
Here is part of my code:
If dOutput = 1 Then
GoTo Time
GoTo Chart
ElseIf dOutput = 2 Then
GoTo Time
ElseIf dOutput = 3 Then
GoTo Chart
End If
Time:
'This is really long, so I left it out...
Chart:
frmChart.Show
Anyone know why?
-
Apr 4th, 2001, 05:52 PM
#2
Frenzied Member
First of all, try to stay away from GoTos!!! Have you heard the phrase "spaghetti code?"? Goto statements are a big cause of this very hard-to-read code.
The way to "go to" subs is simply to call the sub's name.
example:
Code:
If dOutput = 1 Then
Call Time()
Call Chart ()
ElseIf dOutput = 2 Then
Call Time ()
ElseIf dOutput = 3 Then
Call Chart ()
End If
After the function that you call finishes executing, control will return to the procedure that called it. Thus, in the first if statement, the Time procedure will execute, control will return where it left off, and then the Chart procedure will execute.
Last edited by seaweed; Apr 4th, 2001 at 06:13 PM.
~seaweed
-
Apr 4th, 2001, 06:24 PM
#3
Frenzied Member
Remember if you are using the call there is a big difference between the call and just using the function
ie:
test()
or call Test()
when you use call it emits the return value. Just for FYI
-
Apr 4th, 2001, 06:32 PM
#4
Frenzied Member
P.S. If you insist on using GoTos, the reason that #2 isn't working is that after Time: is executed, the processing coninues to the next line of code. The following might work for you, but again I STRONGLY encourage you to break out your Goto's and create sub procedures and functions to handle the processing.
Code:
Private Sub MySub ()
If dOutput = 1 Then
GoTo Time
GoTo Chart
ElseIf dOutput = 2 Then
GoTo Time
ElseIf dOutput = 3 Then
GoTo Chart
End If
Exit Sub
Time:
' Do your time stuff here
Resume ' Not sure if this will work, but may bring control back to If statement
Exit Sub
Chart:
' Do your stuff here
Resume
End Sub
Im not sure if the "Resume" will work (bring control back to the if statement). The thing about Gotos it that when you goto one statement, execution keeps going until it gets to an Exit Sub/Function or End Sub/Function statement. That's why it kept on going in your second if statement.
-
Apr 4th, 2001, 06:38 PM
#5
Frenzied Member
GOTO's where left in VB for the backwards compatablity issue and for the old time programs that were used to this way of executing code.
-
Apr 4th, 2001, 06:39 PM
#6
Frenzied Member
Originally posted by jjortiz
Remember if you are using the call there is a big difference between the call and just using the function
ie:
test()
or call Test()
when you use call it emits the return value. Just for FYI
Good call, jjortiz.
You're right. In fact, I normally don't use Call at all. Call is usually used for SubProcedures (which don't have a return value). The only thing it does is let you put parenthesis around your arguments.
The MsgBox function is a good example:
Code:
' this acts like a sub...no return value
MsgBox "Hello"
' this is the same as above, only you are using parenthesis
Call MsgBox ("Hello") ' Still no return value
' Using parenthesis with no Call treats the procedure as
' a function instead of a sub, and will return a value
RetVal = MsgBox ("Hello")
I just used Call so I could use parenthesis to make it more obvious that I was calling a sub procedure.
-
Apr 4th, 2001, 06:40 PM
#7
Frenzied Member
Originally posted by jjortiz
GOTO's where left in VB for the backwards compatablity issue and for the old time programs that were used to this way of executing code.
...and they should be avoided like the plague (except in error handling, when they are quite convenient)!
-
Apr 4th, 2001, 06:42 PM
#8
Frenzied Member
That's about the only thing that they are good for. I never say try to stay away from them if you can. I say stay away from them unless you are doing error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|