Restart a Sub and keep loop going

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

In a sub procedure how do I recall/restart the sub and maintain the position
in a loop?

Eg (this doesnt work how I need it to work)...

sub Get_Language()

'code here to prompt the user to do something
'...

dim i as integer
dim k as whatever
for i = 1 to 3
if whatever <> "someval" and i < 3 then
'restart this sub and prompt the user to do something
elseif whatever <> "someotherval" and i < 3 then
'restart this sub and prompt the user to do something
elseif i = 3 then
msgox("sorry, exiting app now")
end if
next i

end sub

I need to do the "'code here to prompt the user to do something" part at the
beginning of the sub and everytime the sun is called/recalled, however, the
variable i i not maintained because the sub is restarting. If I seperate
the "'code here to prompt the user to do something" part and call it instead
of the sub itself everything is fine... I just rather have both in the same
sub. I this a possibility?

Thanks a lot.

Jay
 
Jay said:
In a sub procedure how do I recall/restart the sub and maintain the position
in a loop?

Eg (this doesnt work how I need it to work)...

sub Get_Language()

'code here to prompt the user to do something
'...

dim i as integer
dim k as whatever
for i = 1 to 3
if whatever <> "someval" and i < 3 then
'restart this sub and prompt the user to do something
elseif whatever <> "someotherval" and i < 3 then
'restart this sub and prompt the user to do something
elseif i = 3 then
msgox("sorry, exiting app now")
end if
next i

end sub

I need to do the "'code here to prompt the user to do something" part at the
beginning of the sub and everytime the sun is called/recalled, however, the
variable i i not maintained because the sub is restarting. If I seperate
the "'code here to prompt the user to do something" part and call it instead
of the sub itself everything is fine... I just rather have both in the same
sub. I this a possibility?

Thanks a lot.

Jay

Restarting the sub could sort of be done with recursion, but it may not
make sense in your case.

I don't know what the "prompt user to do something" part is all about,
or what you're trying to accomplish. Here's how I would suggest to do
it:

public function prompt_user(args) as prompt_result
'prompt user to do something and return the result.
end function

sub Get_Language()
dim result as prompt_result = prompt_user(args)
dim i as integer
for i = 1 to 3
if result <> "someval" and i < 3 then
result=promp_user(args)
i=1 'reset the counter to restart the loop.
elseif result <> "someotherval" and i < 3 then
result=promp_user(args)
i=1
elseif i = 3 then
msgox("sorry, exiting app now")
end if
next i
end sub

True, this could probably be cleaned up and improved upon as well.
you could also re-write the for-loop as a while:
while not done and i <= 3
if result <> "someval" and i < 3 then
result=promp_user(args)
i=0 'reset the counter to restart the loop.
elseif result <> "someotherval" and i < 3 then
result=promp_user(args)
i=0
elseif i = 3 then
msgox("sorry, exiting app now")
done = true
end if
i+=1
next
Hope this is a start.
 
You could (for example you could pass the index to the sub as an argument
and start from here) but IMO the recursive call would just obscur the
purpose.
You may want to explain what you are trying to do...

My first thought would be that you are trying to do something like :

Do While IsBad and TryCount<=3
 
My first thought would be that you are trying to do something like :
Do While IsBad and TryCount<=3

I'm not sure if it's what you meant here, but one approach would be to
wrap the code in a do...loop and call "continue do" whenever you needed
to start the code over. Remembering of course to call "exit do" or
using some sort of exit counter to prevent infinite loop situations.

Thanks,

Seth Rowe
 
Jay,

Luckily goto commands don't exist anymore, however setting the part that has
to be done more times can be best in a seperate method.

Cor
 
Jay said:
In a sub procedure how do I recall/restart the sub and maintain the position
in a loop?

Eg (this doesnt work how I need it to work)...

sub Get_Language()

'code here to prompt the user to do something
'...

dim i as integer
dim k as whatever
for i = 1 to 3
if whatever <> "someval" and i < 3 then
'restart this sub and prompt the user to do something
elseif whatever <> "someotherval" and i < 3 then
'restart this sub and prompt the user to do something
elseif i = 3 then
msgox("sorry, exiting app now")
end if
next i

end sub

It seems you want to prompt the user at most three times if some
condition is not holding.

If so, then the sequence of actions in your code is not correct. What
you should be doing is:
sub Get_Language()

Set Ok to False
Set Tries = 0
Repeat Until Ok OrElse Tries = 3
Prompt the user
If all conditions are met then set Ok to True
Tries += 1
End of Repeat
If Not Ok then Give Up with an error message

Which might be translated to the following aircode (i.e., not tested):

Sub Get_Language()
Dim Ok As Boolean
For Tries As Integer = 1 To 3
'...
'-- Prompt the user
'...
If Whatever = "someval" _
OrElse Whatever = "someotherval" Then
Ok = True
Exit For
End If
Next
If Not Ok Then MsgBox("sorry, exiting app now")
'...
'-- Clean up
'...
End Sub

HTH.

Regards,

Branco.
 
Back
Top