473,805 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I exit from a recursive subroutine?

I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.Ind exOf("#includev irtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.Ind exOf("#includev irtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.Ind exOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.Ind exOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.Ind exOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace , Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClo se() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.Ind exOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items. Add(filename & "
includes: " & Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items. Add(filename & " includes: "
& Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items. Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items. Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #1
3 2803
I didn't look in detail at your code but you exit a recursive routine when
you do a Return or Exit Sub without calling the Function or Sub before, i.e.,
see some code below that I think will work. Notice if I>=10, the DoSomething
does not call itself again. Be careful with recursive routines as you can
get an out-of-Stack error very easily.

Private Sub DoSomething( )
Static i as integer
i +=1
if i <10 then DoSomething else exit sub
end sub

--
Dennis in Houston
"Ameen" wrote:
I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.Ind exOf("#includev irtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.Ind exOf("#includev irtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.Ind exOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.Ind exOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.Ind exOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace , Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClo se() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.Ind exOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items. Add(filename & "
includes: " & Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items. Add(filename & " includes: "
& Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items. Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items. Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #2
Ameen,

If your search on a string is so complex, than there is for that Regex

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

I hope this helps a little bit?

Cor
"Ameen" <Am*****@gmail. com> schreef in bericht
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.Ind exOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.Ind exOf("#includev irtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.Ind exOf("#includev irtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.Ind exOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.Ind exOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.Ind exOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace , Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClo se() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.Ind exOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items. Add(filename & "
includes: " & Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items. Add(filename & " includes: "
& Mid(LineNoSpace , Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items. Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items. Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #3
Hi Ameen,

Don't mind, but your code does not appear to be the best way to do
whatever it intends to do. If you give more details about exactly what
you intend to do, it might help others to give you valid suggestions /
alternatives.

Although your code is well commented, I'm still unsure about what
exactly it does. An eagle-eye examination of the code suggests that
this might be better done with Regex.

Regards,

Cerebrus.

Apr 2 '06 #4

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

Similar topics

4
3734
by: deko | last post by:
When I loop through this function, it works fine until it hits End Function - then it jumps to End Select. Very strange... This behavior occurs when Case = 255. Any ideas why this is happening? Why doesn't the function just end? Why the jump back in at End Select? Thanks in advance. Private Function GetRecurrenceType(r As Long, Optional s As Boolean) As String Dim strReturn As String
14
1917
by: BQ | last post by:
Due to a lack of resources, I have to translate the following recursive function in its iterative form. It's a kind of dichotomic search. void SearchSlaves(unsigned long start_uid, unsigned long end_uid) { char ret; //ping over a range of addresses (all slaves with uid in the range from start_uid to end_uid will reply) ret = PingSlave(start_uid,end_uid);
17
8067
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which situation? Also I was wondering if it whould be wise to combine the standard status with return. exp:
2
1434
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! **************************************************************************************************** Original Question: -------------------- Has anyone called a COBOL subroutine using COBOL CALL from a COBOL/DB2
10
1892
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the report header and STDOUT prints the report data. Both these subroutine runs fine individually, independent of each other. Now I have included both these subroutine in my main program and I call these subroutine depending upon the name of the report. Here...
16
1660
by: jayapal | last post by:
hi all, what is the differrence b/w the usage or return and the exit in the C programming.. thanks, jay
11
2851
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
6
2208
by: Michael_R_Banks | last post by:
I have a program with a user input subroutine that I want to exit after a specific amount of time. How do I exit the sub when the timer elapsed event fires? Regards, Michael
39
2838
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main' function into 'mymain', but I am stuck as to what I should do for the 'exit'. AFAIK there is no portable way to catch the exit. The only thing I can think of is atexit, but that does not work since 'exit' is still called afterward.
0
9718
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
10614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
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...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
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.