Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > What is wrong with this simple function?


Reply
 
Thread Tools Display Modes
  #1  
Old 10-13-2005, 03:28 PM
xcannon xcannon is offline
Freshman
 
Join Date: Oct 2004
Posts: 32
Default What is wrong with this simple function?


I made this function, and 50% of the time when I go to use it, it freezes up. My goal was to make a funcion that gets EVERYTHING between two strings. Here it is:

Code:
Public Function GrabItems(ByVal strSource As String, ByVal strStart As String, ByVal strEnd As String, ByVal listAdd As Object) As Boolean
Dim lnCurrentPos As Long
Dim lnTextStart As Long
Dim lnTextEnd As Long
Dim bolContinue As Boolean
Dim strFound As String

    bolContinue = True
    
lnCurrentPos = InStr(1, strSource, strStart, vbTextCompare)
If lnCurrentPos > 0 Then
    Do Until bolContinue = False
        lnTextStart = lnCurrentPos + Len(strStart)
        lnTextEnd = InStr(lnTextStart, strSource, strEnd, vbTextCompare)
        If lnTextEnd > 0 And lnTextEnd > lnTextStart Then
            strFound = Mid(strSource, lnTextStart, lnTextEnd - lnTextStart)
            listAdd.AddItem strFound
            lnCurrentPos = InStr(lnTextEnd + Len(strEnd), strSource, strStart, vbTextCompare)
            If lnCurrentPos < lnTextStart Then
                bolContinue = False
            End If
        Else
            bolContinue = False
        End If
    Loop
End If
End Function
Reply With Quote
  #2  
Old 10-13-2005, 03:53 PM
LaVolpe's Avatar
LaVolpe LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

I don't think your routine is locking up, it is probably going into an infinite loop.

The only place that can happen is in the following location: add the following check and keep your debug/immediate window open. When the same value is constantly being written, hit Ctrl+Break to break your code. The end result is that you may find that your tracking calculations may need to be adjusted.

Code:
If lnCurrentPos < lnTextStart Then bolContinue = False Else ' Since there is no setting of bolContinue=False in this part of the IF, this is the ' place that most likely your infinite loop is happening. Debug.Print "lnCurrentPos = "; lnCurrentPos ' ^^ When this line continually prints the same thing, you have an infinite loop End If
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->