My application is supposed to do the following:
- Accept a file for input, each line contains 1 record
- Validate the records
- Strip any invalid records
- Pass the remaining data to the database
I'm running into a weird issue where my file contains multiple lines, and if I use the Data Visualizer in Visual Studio the string contains multiple lines, but when I attempt to store the result of String.Split
into an array (splitting on \r\n
) I only get a single element in my array. Here's a screenshot of my Watch tab:
The first row is my fileContents
variable, a string
If I use the Text Visualiser, you can see it's broken out onto separate lines. If I copy and paste that data into note pad, we can see the carriage return & line feed.
The line below that is the fileData
array, which populated using String.Split
Here is the actual code:
Private Sub cmdImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdImport.Click
Dim fileContents As String = GetFileData(pStrBaseDir & pStrFileName)
Dim linesToExclude As List(Of Int16) = New List(Of Int16)
mfpScanFile(pStrBaseDir & pStrFileName, pErrorString, pRtfErrString, pStrOutput, linesToExclude)
'mfpScanFile loops over the file and validates the records, adding the row # to linesToExclude if the row is bad
'Here we attempt to remove the bad rows
'First we split the string on new lines into an array
'Then we clear each line specified by linesToExclude
Dim splitter As String() = {"\r\n"}
Dim fileData As String() = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
For i As Int16 = 0 To linesToExclude.Count - 1
fileData(linesToExclude(i)) = ""
Next
fileContents = String.Join("\r\n", fileData)
End Sub
Private Function GetFileData(ByVal strBaseDir As String) As String
If (Not System.IO.File.Exists(strBaseDir)) Then
GetFileData = String.Empty
Exit Function
End If
Dim sb As StringBuilder = New StringBuilder()
For Each line As String In System.IO.File.ReadAllLines(strBaseDir)
Dim elements As String() = line.Split(",")
If (Not elements.Length = 15) Then
GetFileData = "BadCommaCount"
Exit Function
End If
sb.AppendLine(line)
Next
GetFileData = sb.ToString()
End Function
So the problem I'm having is that my For
loop throws an exception on this line: fileData(linesToExclude(i)) = ""
It throws an exception because fileData
only has 1 element. But why it only has 1 element is what I don't understand. The Watch window shows my strings as a single line, but the visualer shows it has line feeds so why isn't my split working?
Furthermore, I have this almost-exact same code in C# and it handles the same file perfectly:
List<int> linesToExclude = new List<int>();
strBadRecs = ScanFile(strBaseDir, ref strErrorString, ref strRtfErrString, ref strOutput, ref linesToExclude);
// Stripping out bad records
string[] splitter = {"\r\n"};
string[] fileData = objDemographicImport.FileData.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < linesToExclude.Count; i++)
{
fileData[linesToExclude[i]] = String.Empty;
}
So what am I doing wrong?
GetFileData
return a string collection, instead of putting together the collection, joining it into a single string and then splitting it out again? – A Friend