By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,909 Members | 557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,909 IT Pros & Developers. It's quick & easy.

Parsing Text File doesn't pickup all If statements

P: 20
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
Expand|Select|Wrap|Line Numbers
  1.         If File.Exists(varIni) Then
  2.             Dim readFile As System.IO.TextReader = New StreamReader(varIni)
  3.             Dim line As String
  4.             While readFile.Peek > -1
  5.                 line = readFile.ReadLine()
  6.                 If line.Substring(0, 9) = "CompName=" Then
  7.                     varCompName = line.Substring(9, line.Length - 9)
  8.                     TextBox5.Text = varCompName
  9.                 End If
  10.                 If line.Substring(0, 12) = "PhoneNumber=" Then
  11.                     varPhoneNum = line.Substring(12, line.Length - 12)
  12.                     TextBox6.Text = varPhoneNum
  13.                 End If
  14.                 If line.Substring(0, 11) = "WebAddress=" Then
  15.                     varWebAdd = line.Substring(11, line.Length - 11)
  16.                     TextBox7.Text = varWebAdd
  17.                 End If
  18.  
  19.                 'If line.Substring(0, 13) = "EmailAddress=" Then
  20.                 '    varMailAdd = line.Substring(13, line.Length - 13)
  21.                 '    TextBox8.Text = varMailAdd
  22.                 'End If
  23.  
  24.                 If line.Substring(0, 11) = "EmailLogin=" Then
  25.                     varMailLog = line.Substring(11, line.Length - 11)
  26.                     TextBox9.Text = varMailLog
  27.                 End If
  28.                 If line.Substring(0, 10) = "EmailPass=" Then
  29.                     varMailPass = line.Substring(10, line.Length - 10)
  30.                     TextBox10.Text = varMailPass
  31.                 End If
  32.                 If line.Substring(0, 11) = "HourlyRate=" Then
  33.                     varHourly = line.Substring(11, line.Length - 11)
  34.                     TextBox11.Text = varHourly
  35.                 End If
  36.                 If line.Substring(0, 9) = "MealRate=" Then
  37.                     varMeal = line.Substring(9, line.Length - 9)
  38.                     TextBox12.Text = varMeal
  39.                 End If
  40.             End While
  41.             readFile.Close()
  42.             readFile = Nothing
  43.         Else
  44.             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)
  45.         End If
  46.  
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
Jun 4 '11 #1
Share this Question
Share on Google+
2 Replies

P: 20
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
Jun 4 '11 #2

P: 20
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.
Sep 30 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.