Parsing Text File doesn't pickup all If statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • finked1973
    New Member
    • Jun 2009
    • 20

    Parsing Text File doesn't pickup all If statements

    Hi All

    I have a piece of code in my application that is giving me a funny result. I am reading a text file and based on it's first word on the line I have a bunch of IF statements that then assign the value to a variable.
    The problem is that not all the if statements are being met, well they are but that's what's weird.
    Here's my code and I'll explain
    Code:
            If File.Exists(varIni) Then
                Dim readFile As System.IO.TextReader = New StreamReader(varIni)
                Dim line As String
                While readFile.Peek > -1
                    line = readFile.ReadLine()
                    If line.Substring(0, 9) = "CompName=" Then
                        varCompName = line.Substring(9, line.Length - 9)
                        TextBox5.Text = varCompName
                    End If
                    If line.Substring(0, 12) = "PhoneNumber=" Then
                        varPhoneNum = line.Substring(12, line.Length - 12)
                        TextBox6.Text = varPhoneNum
                    End If
                    If line.Substring(0, 11) = "WebAddress=" Then
                        varWebAdd = line.Substring(11, line.Length - 11)
                        TextBox7.Text = varWebAdd
                    End If
    
                    'If line.Substring(0, 13) = "EmailAddress=" Then
                    '    varMailAdd = line.Substring(13, line.Length - 13)
                    '    TextBox8.Text = varMailAdd
                    'End If
    
                    If line.Substring(0, 11) = "EmailLogin=" Then
                        varMailLog = line.Substring(11, line.Length - 11)
                        TextBox9.Text = varMailLog
                    End If
                    If line.Substring(0, 10) = "EmailPass=" Then
                        varMailPass = line.Substring(10, line.Length - 10)
                        TextBox10.Text = varMailPass
                    End If
                    If line.Substring(0, 11) = "HourlyRate=" Then
                        varHourly = line.Substring(11, line.Length - 11)
                        TextBox11.Text = varHourly
                    End If
                    If line.Substring(0, 9) = "MealRate=" Then
                        varMeal = line.Substring(9, line.Length - 9)
                        TextBox12.Text = varMeal
                    End If
                End While
                readFile.Close()
                readFile = Nothing
            Else
                MessageBox.Show("You will need to configure your application, please go to the Configuration tab and fill in the details for your business", "xlcr IT Solutions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
    You will notice that half way down the IF statements there is one block that is commented out, if I leave this commented out all values are assigned, if I uncomment the block the last two variables are not assigned.

    Can someone shed any light as to why this is happening ?

    Thanks in advance
  • finked1973
    New Member
    • Jun 2009
    • 20

    #2
    Don't worry about this, I managed to do a workaround, I took out the = signs in the if statement eg

    If line.Substring( 0, 10) = "EmailLogin " Then
    varMailLog = line.Substring( 11, line.Length - 11)
    TextBox9.Text = varMailLog
    End If

    God knows why it wasn't working but I've spent too long on it

    Cheers

    Comment

    • finked1973
      New Member
      • Jun 2009
      • 20

      #3
      Finally understand why

      If anyone else has this problem, here is my understaning of it.

      When you substring a line in a reader function and run that through a block of if comparisons if the substring value is for example 12 and it hits a line in the fil of say 8, that line of 8 is too short for the comparison of 12. This causes the code to error.
      To sort any comparison block substrings should be the same size for it to work correctly.

      Comment

      Working...