Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GoTo - not for error control 1

Status
Not open for further replies.

lbrechler

Programmer
May 17, 2001
44
US
Ok, in general, I know to avoid GoTo - my instructors all said the same thing. BUT...

I'm working on a program, and I want to timeout and move on after a given period of user inactivity. So, when I present a question to a user I set the interval and start (enable) a timer object. If the user answers, I turn the timer off (enable = False), but if the user doesn't answer, it automatically goes into the Timer1_Timer function. From there, i want to go back to a different section of my code, bypassing a section. Is there any way to do this WITHOUT a GoTo? I'm open to suggestions, but just can't think of anything else myself.

Thanks in advance.

~Lindsay

P.S. If GoTo IS the right thing to do, I tried using one, but I get the error "Compile Error: Label Not Defined" Any suggestions for this?
 
You can use Goto if you wish. Your label not defined error is because Vb can't find the label in your goto statement

GoTo ThisSpot
The above statement is looking for the label

ThisSpot:

Labels for goto are defined with a colon and are case sensitive.

If you want to avoid a goto then you will probably have to break up your code into smaller sub procedures and call them accordingly. But sometimes a goto is easier. Thanks and Good Luck!

zemp
 
Well, I checked the spelling and capitalization (even used "ThisSpot" =) ), but I think that maybe the problem is that it's looking for the label within the Timer1_Timer function, but the code I need to go to is in a different section (of the same form's) code - actually in a different Sub. I even tried marking the other Sub Public instead of Private, but to no avail. Any suggestions?

~Lindsay
 

if someeventdidnothappen then

call somesub(false)

else

call somesub(true)

endif


private sub somesub(DidEventhappen as boolean)

if dideventhappen = true then

'do this

else

'do this

endif

'this code will be executed every time

end sub

I think you can get the idea from this

Good Luck
 
it might be easier to have the point where you enable the Timer to be the end of that sub. that way the flow of the program stops at that point, and allows you to specify the next branch cleanly. VB will continue executing your sub after you enable the Timer, which could cause some unnecessary confusion.

it may also be cleaner to have the Timer control itself and control where the flow of execution goes next. you could do this by using Public(or just a private declared in the General Declarations of a Form) for the time interval.

Say you want to wait 10 seconds, or 10,000 ms. You could have the Timer set for intervals of 100ms, and add 100 to the public variable at the end of the Timer sub.

Then you can use the Timer to check if the question has been answered at 100ms intervals with an IF statement


If Len(Answer$) <> 0 Then

Timer.Enabled = False
NameofSubHere
Endif

and,

If PublicTimerVar = 10000 Then

'10 seconds has elapsed.. process Timeout sub
Endif


PublicTimerVar = PublicTimerVar + 100 'time measured in ms.

End Sub






 
You will have to throw a DoEvents in that &quot;wait&quot; loop if you ever expect the user to be able to enter any input and for the Timer event to be fired...and you must prevent any other user action on other controls during the DoEvents...especially those that could lead right back to the DoEvents. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Just a little FYI... As you may have noticed but not realized, you cannot branch into different Sub-Routines or Functions with a GoTo. Your GoTo can only branch to code in the Sub or Function it was placed in.

GoTo commands are generally a poor programming habit. However, VB has eliminated a lot of the pitfalls of these branches by restricting where a GoTo is allowed to go. Although GoTo commands are still not the best technique to use in most cases, they can still be your friend and can be useful.
--
Jonathan
 
Jonathan,

I had pretty much resigned myself to the fact that you can't go into other Sub-Routines or functions, but thank you for confirming it for me.

Now I'll just work on restructuring the program to call another subroutine from the timer function.

Thanks again!

~Lindsay
 
Sorry, I had not intended for that post to point directly at you. I assumed you knew that now by either experience or reading elsewhere. I really intended it to be for anyone else who may not have known. The wording was off on my part.

Again, sorry for the confusion.
--
Jonathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top