473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restart a Sub and keep loop going

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 <"someotherva l" 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

Nov 22 '06 #1
5 9016

Jay wrote:
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 <"someotherva l" 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(arg s) as prompt_result
'prompt user to do something and return the result.
end function

sub Get_Language()
dim result as prompt_result = prompt_user(arg s)
dim i as integer
for i = 1 to 3
if result <"someval" and i < 3 then
result=promp_us er(args)
i=1 'reset the counter to restart the loop.
elseif result <"someotherva l" and i < 3 then
result=promp_us er(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_us er(args)
i=0 'reset the counter to restart the loop.
elseif result <"someotherva l" and i < 3 then
result=promp_us er(args)
i=0
elseif i = 3 then
msgox("sorry, exiting app now")
done = true
end if
i+=1
next
Hope this is a start.

Nov 22 '06 #2
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

--
Patrice

"Jay" <so*****@somewh ere.coma écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 6.phx.gbl...
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 <"someotherva l" 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

Nov 22 '06 #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
Patrice wrote:
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

--
Patrice

"Jay" <so*****@somewh ere.coma écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 6.phx.gbl...
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 <"someotherva l" 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
Nov 23 '06 #4
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" <so*****@somewh ere.comschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
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 <"someotherva l" 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

Nov 23 '06 #5
Jay wrote:
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 <"someotherva l" 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 = "someotherv al" Then
Ok = True
Exit For
End If
Next
If Not Ok Then MsgBox("sorry, exiting app now")
'...
'-- Clean up
'...
End Sub

HTH.

Regards,

Branco.

Nov 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
14500
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in findListBox.listBox2.Items) { listBox2.Items count changed, restart this foreach } Thanks for any help.
18
5374
by: amdishigh | last post by:
We are experiencing the following problems concerning ASP.NET: our ASP.NET application, written in .NET 1.1 platform, and hosted in IIS 5.0 environment, experience frequent session timeout problem. After tracing, we find that it should be caused by "ASP.NET Application Restart" (proved by incremented counter for "Application Restarts" of "ASP.NET 1.1.4322" in "Performance" whenever the timeout happens). From MSDN documentation, we find...
5
1808
by: Daniel | last post by:
Hi, is the a way to prevent aspnet process to restart after web.config is modified? like, waiting for all sessions to close, or not restart at all ? Thank you, Daniel
6
3808
by: Leonardo Curros | last post by:
Hello, I would like to know what's the best way to restart one service. I would like to do it from the service itself. Is this possible? I try it with ServiceController.stop() ServiceController.WaitForStatus(ServiceControllerStatus.Stopped) ServiceController.start() but doesn´t works. It seems waitforstatus instruction is the last
1
5891
by: Joe | last post by:
This is probably really simple... (I'm not a vb programmer) I have a For loop I want to restart from within the loop. For i = 0 To myarray.Length - 1 .... .... If something Then myarray = a new array i = 0 End If
5
2105
by: juky | last post by:
Hi all, I have 3 running threads chencking for something in my application. Based on that I need to stop some of them safely wait for something else and restart. Sleep is not good in my case because i don't know how much to wait. How and from where I can put a thread in pause an restart it? Thank you Juky
1
3406
by: Steve | last post by:
I need to restart my VB.NET 2.0 app but I need to delay the new instance's start until I know the previous instance has ended and freed up all use of its resources (DLLs). What I have is an app where the EXE is basically a shell and there are multiple DLL "plug-in" modules. Within the shell I have a "load new module" function which lets the user import new plug-ins. When an updated plug-in is loaded, I can't overwrite the DLL immediately...
3
2841
by: martin1 | last post by:
Hi, All, I want user select first item (called All) in listbox, then all other items are selected by SetSelected method, but in loop (see code below) whenever going to SetSelected(), the SelectedIndexChanged event keep fire and loop doesn't go to next, finally the program stop at infinite loop. Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCem.SelectedIndexChanged If...
4
7180
by: yxq | last post by:
Hello, I want to kill the Explorer process, after Explorer process has been killed, it will restart automatically immediately. How to disable it restart automatically? for example, in Windows Task Manager, you can kill explorer process, but it will not restart automatically. My code: ///////////////////////////////////////////// For Each ObjPro As Process In Process.GetProcessesByName("EXPLORER")
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.