D
dwyee
Guest
I am writing a Windows app in VB2010, and I have a "Setup" text file, each entry delimited by a vbTab, that Im trying to read into a 2D array. Each line has either 25 fields, or one field, and there will be 10 lines in each file. Whats the best way to (a) dynamically dimension my input array (ReaderArray), and (b) step thru each field of each line?
Im attempting to use TextFieldParser and ReadFields to read the files contents into a 2D array, but I dont understand how to step thru each line and each field.
Heres the code Ive got so far:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
tbFileName.Text = OpenFileDialog1.FileName
GSLoadFileName = OpenFileDialog1.FileName
Dim GSSetupReader As Microsoft.VisualBasic.FileIO.TextFieldParser
GSSetupReader = My.Computer.FileSystem.OpenTextFieldParser(GSLoadFileName)
GSSetupReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
GSSetupReader.SetDelimiters(vbTab)
GSSetupReader.TrimWhiteSpace = True
Dim currentRow As String()
Dim ReaderArrayLength As Int32 = 0
Dim ReaderArray()
While Not GSSetupReader.EndOfData
Try
currentRow = GSSetupReader.ReadFields()
Dim currentField As String
ReaderArrayLength = currentRow.GetLength(0)
the first line here checks for the first line being only ONE field long (it contains an integer 1-12, representing each crop type),
and the final line of the file has the contents of a multiline textbox (again, just ONE field)
If ReaderArrayLength = 1 And currentRow(0) <> "Growth1" And currentRow(0) <> "Growth2" And currentRow(0) <> "Growth3" And currentRow(0) <> "Bloom1" _
And currentRow(0) <> "Bloom2" And currentRow(0) <> "Bloom2" And currentRow(0) <> "Bloom3" And currentRow(0) <> "Flush" And Len(currentRow(0)) > 1 Then
currentField = currentRow(0)
Select Case currentField
Case 1
driver.GRCropType = "Soybean"
Case 2
driver.GRCropType = "Tomato"
Case 3
driver.GRCropType = "Lettuce"
Case 4
driver.GRCropType = "Swiss Chard"
Case 5
driver.GRCropType = "Radish"
Case 6
driver.GRCropType = "Sweet Pepper"
Case 7
driver.GRCropType = "Jalapeno Pepper"
Case 8
driver.GRCropType = "Beet"
Case 9
driver.GRCropType = "Pea"
Case 10
driver.GRCropType = "Green Bean"
Case 11
driver.GRCropType = "Cabbage"
Case 12
driver.GRCropType = "Bok Choy"
Case Else
driver.GRCropType = "UNKNOWN"
End Select
ReaderArray(0,0) = currentField
Else
For Each currentField In currentRow
**I think this is where I step thru each field in each row(line), then step thru each row(line) of the file********
**what exactly do I do here???***************************************************************
Next
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Reading File!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End While
GSSetupReader.Close()
End Sub
Continue reading...
Im attempting to use TextFieldParser and ReadFields to read the files contents into a 2D array, but I dont understand how to step thru each line and each field.
Heres the code Ive got so far:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
tbFileName.Text = OpenFileDialog1.FileName
GSLoadFileName = OpenFileDialog1.FileName
Dim GSSetupReader As Microsoft.VisualBasic.FileIO.TextFieldParser
GSSetupReader = My.Computer.FileSystem.OpenTextFieldParser(GSLoadFileName)
GSSetupReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
GSSetupReader.SetDelimiters(vbTab)
GSSetupReader.TrimWhiteSpace = True
Dim currentRow As String()
Dim ReaderArrayLength As Int32 = 0
Dim ReaderArray()
While Not GSSetupReader.EndOfData
Try
currentRow = GSSetupReader.ReadFields()
Dim currentField As String
ReaderArrayLength = currentRow.GetLength(0)
the first line here checks for the first line being only ONE field long (it contains an integer 1-12, representing each crop type),
and the final line of the file has the contents of a multiline textbox (again, just ONE field)
If ReaderArrayLength = 1 And currentRow(0) <> "Growth1" And currentRow(0) <> "Growth2" And currentRow(0) <> "Growth3" And currentRow(0) <> "Bloom1" _
And currentRow(0) <> "Bloom2" And currentRow(0) <> "Bloom2" And currentRow(0) <> "Bloom3" And currentRow(0) <> "Flush" And Len(currentRow(0)) > 1 Then
currentField = currentRow(0)
Select Case currentField
Case 1
driver.GRCropType = "Soybean"
Case 2
driver.GRCropType = "Tomato"
Case 3
driver.GRCropType = "Lettuce"
Case 4
driver.GRCropType = "Swiss Chard"
Case 5
driver.GRCropType = "Radish"
Case 6
driver.GRCropType = "Sweet Pepper"
Case 7
driver.GRCropType = "Jalapeno Pepper"
Case 8
driver.GRCropType = "Beet"
Case 9
driver.GRCropType = "Pea"
Case 10
driver.GRCropType = "Green Bean"
Case 11
driver.GRCropType = "Cabbage"
Case 12
driver.GRCropType = "Bok Choy"
Case Else
driver.GRCropType = "UNKNOWN"
End Select
ReaderArray(0,0) = currentField
Else
For Each currentField In currentRow
**I think this is where I step thru each field in each row(line), then step thru each row(line) of the file********
**what exactly do I do here???***************************************************************
Next
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Reading File!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End While
GSSetupReader.Close()
End Sub
Continue reading...