Home All Groups Group Topic Archive Search About

how can I exit from a recursive subroutine?

Author
2 Apr 2006 7:47 AM
Ameen
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.IndexOf("<!--", 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.IndexOf("<!--", indexstart)
        End While
        If Pos1 <> -1 Then ' first comment was found, continue
            Pos2 = LineNoSpace.IndexOf("#includevirtual", 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.IndexOf("#includevirtual",
indexstart)
            End While
            If Pos2 <> -1 Then                              'if virtual
include is found, check for the first double quotes
                Pos3 = LineNoSpace.IndexOf("""", 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.IndexOf("""", indexstart)
                End While
                If Pos3 <> -1 Then 'if first double quotes are found
then
                    Pos4 = LineNoSpace.IndexOf("""", 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
                        CheckCommentClose() '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.IndexOf("<!--", 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

Author
2 Apr 2006 2:24 PM
Dennis
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


Show quoteHide quote
"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.IndexOf("<!--", 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.IndexOf("<!--", indexstart)
>         End While
>         If Pos1 <> -1 Then ' first comment was found, continue
>             Pos2 = LineNoSpace.IndexOf("#includevirtual", 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.IndexOf("#includevirtual",
> indexstart)
>             End While
>             If Pos2 <> -1 Then                              'if virtual
> include is found, check for the first double quotes
>                 Pos3 = LineNoSpace.IndexOf("""", 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.IndexOf("""", indexstart)
>                 End While
>                 If Pos3 <> -1 Then 'if first double quotes are found
> then
>                     Pos4 = LineNoSpace.IndexOf("""", 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
>                         CheckCommentClose() '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.IndexOf("<!--", 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
>
>
Author
2 Apr 2006 2:45 PM
Cor Ligthert [MVP]
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


Show quoteHide quote
"Ameen" <Amee***@gmail.com> schreef in bericht
news:1143964054.831682.181600@i40g2000cwc.googlegroups.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.IndexOf("<!--", 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.IndexOf("<!--", indexstart)
>        End While
>        If Pos1 <> -1 Then ' first comment was found, continue
>            Pos2 = LineNoSpace.IndexOf("#includevirtual", 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.IndexOf("#includevirtual",
> indexstart)
>            End While
>            If Pos2 <> -1 Then                              'if virtual
> include is found, check for the first double quotes
>                Pos3 = LineNoSpace.IndexOf("""", 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.IndexOf("""", indexstart)
>                End While
>                If Pos3 <> -1 Then 'if first double quotes are found
> then
>                    Pos4 = LineNoSpace.IndexOf("""", 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
>                        CheckCommentClose() '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.IndexOf("<!--", 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
>
Author
2 Apr 2006 11:35 PM
Cerebrus
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.