Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I use the following code to create and populate an array

Dim objSR1 As StreamReader, oldSize1 As Integer, str6 As String
       With OpenFileDialog1
           .DefaultExt = "txt"
           .FileName = strFileName
           .Filter = "Text Files (*.txt)/*.txt/All Files (*.*)/*.*"
           .FilterIndex = 1
           .InitialDirectory = "C:\Temp\"
           .Title = "Open Existing text file"
       End With
       OpenFileDialog1.ShowDialog()
       Dim str As System.IO.Stream
       str = OpenFileDialog1.OpenFile
       objSR1 = New StreamReader(str)
       Do While objSR1.Peek <> -1
           If Open2 Is Nothing Then
               oldSize1 = -1
           Else
               oldSize1 = UBound(Open2)
           End If
           ReDim Preserve Open2(oldSize1 + 1)
           Open2(oldSize1 + 1) = objSR1.ReadLine
       Loop
       objSR1.Close() 'Close file
       objSR1 = Nothing 'destroy the object

 

 

Then I use the following code to search the array for a key that is set in a buttonclick sub

 

Public Function GetValue(ByVal keyGet As String) As String
       'Given key name keyGet, return corresponding value string
       Dim i As Integer
       Dim keyFound As Boolean
       Dim keyDesc As Boolean
       Dim keyStr As String
       Dim keyStr2 As String
       keyFound = False
       keyDesc = False
       For i = 0 To Open2.Length - 1

           If Not (keyDesc) Then
               keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") )
               If keyStr.Equals(keyGet) Then keyFound = True
               If keyStr.Equals("description") Then
                   keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf(""""))
               End If
               If keyStr.Equals("licence") Then
                   keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf(""""))
               End If
               If keyFound Then
                   GetValue = Open2(i).Substring(Open2(i).IndexOf(" ") + 1)
               End If
           Else
               keyDesc = (Open2(i).IndexOf("""") < 0)
               If keyFound Then
                   GetValue &= Chr(13) & Chr(10) & Open2(i) ' 13 = carriage return 10 = line feed
               End If
           End If
           If keyFound And Not (keyDesc) Then Exit For
       Next i
       If Not (keyFound) Then GetValue = String.Empty
   End Function

 

The problem with the above code is that if it finds a line in the array without a space it returns a value cannot be less than zero error and hightlights this line

keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") )

 

How would I modify the code to skip lines with no spaces?

 

 

Thank you

 

Phil C

  • *Experts*
Posted

Well first, I'd probably rather use XML to store this kind of thing.

 

But, I'd probably not even put the blank lines in my array. I'd first change the top bit of code to the following:

       Dim sLine As String
       Do While objSR1.Peek <> -1
           sLine = objSR1.ReadLine
           If sLine.Length > 0 Then
               If Open2 Is Nothing Then
                   oldSize1 = -1
               Else
                   oldSize1 = UBound(Open2)
               End If
               ReDim Preserve Open2(oldSize1 + 1)
               Open2(oldSize1 + 1) = objSR1.ReadLine
           End If
       Loop

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks Nerseus

I cannot use xml as the text files the code reads from do not have an equal sign separating the keys and their values only a space. I have no control over the arrangement of the text files.

 

Your modification of the code works only if the the line is empty. The problem is if the line contains a word and no space the error occurs.

 

Thanks

 

Phil

  • *Experts*
Posted

Ah, well what do you want to do with lines that don't have a space? Do you need them in your array for something else or can you exclude them? If you want to exclude them, simply change the line in my original Loop to use IndexOf(" ") > 0.

 

Otherwise, why not just do the IndexOf check before trying to use it in the substring?

Public Function GetValue(ByVal keyGet As String) As String
       'Given key name keyGet, return corresponding value string
       Dim i As Integer
       Dim keyFound As Boolean
       Dim keyDesc As Boolean
       Dim keyStr As String
       Dim keyStr2 As String
       keyFound = False
       keyDesc = False
       For i = 0 To Open2.Length - 1

           If Not (keyDesc) Then
               If Open2(i).IndexOf(" ") > 0 Then
                   keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") )
                   If keyStr.Equals(keyGet) Then keyFound = True
                   If keyStr.Equals("description") Then
                       keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf(""""))
                   End If
                   If keyStr.Equals("licence") Then
                       keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf(""""))
                   End If
                   If keyFound Then
                       GetValue = Open2(i).Substring(Open2(i).IndexOf(" ") + 1)
                   End If
End If
           Else
               keyDesc = (Open2(i).IndexOf("""") < 0)
               If keyFound Then
                   GetValue &= Chr(13) & Chr(10) & Open2(i) ' 13 = carriage return 10 = line feed
               End If
           End If
           If keyFound And Not (keyDesc) Then Exit For
       Next i
       If Not (keyFound) Then GetValue = String.Empty
   End Function

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • 5 months later...
Posted

I think if I understand correctly me and you are experiencing the same problems.

 

 

I am trying to get the position of a certain string of text within a string.

 

a simple indexof will do the trick, if its a string with no cr/lf's in it. the indexof function skips over cr/lfs and gives incorrect location of the string.

www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
  • *Experts*
Posted

Egads! IndexOf does NOT skip over CR/LF characters! Twice in one day :)

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...